Return a 302 status code in Spring MVC Controller
Asked Answered
A

3

5

I am developing a Spring MVC app, and I need to check in my controller a certain condition. In case it were true, I have to return a 302 status code. It's something like this:

@RequestMapping(value = "/mypath.shtml", method = RequestMethod.GET)
public ModelAndView pageHandler(@Valid MyForm form, BindingResult result, HttpServletRequest request,
        Locale locale) throws PageControllerException, InvalidPageContextException, ServiceException {

    if (someCondition){
        // return 302 status code
    }
    else{
        // Do some stuff
    }
}

Which is the best way to do this?

Thank you very much in advance

Acatalectic answered 3/6, 2014 at 11:12 Comment(3)
Note that a 302 is not an error, but you still might be able to use Spring's exception framework to map an exception type to 302.Bala
Sorry about it, I have changed the "error" word for status codeAcatalectic
I found the way to do it that works perfectly for me. I posted the answerAcatalectic
A
6

I finally managed to do it using @ResponseStatus, as shown here:

https://mcmap.net/q/117026/-trigger-404-in-spring-mvc-controller

UPDATE

This is the way I finally did it:

@ResponseStatus(value = HttpStatus.MOVED_TEMPORARILY)
public class MovedTemporarilyException extends RuntimeException {

    // ...
}

@RequestMapping(value = "/mypath.shtml", method = RequestMethod.GET)
public ModelAndView pageHandler(@Valid MyForm form, BindingResult result, HttpServletRequest request,
        Locale locale) throws PageControllerException, InvalidPageContextException, ServiceException {

    if (someCondition){
        throw new MovedTemporarilyException();
    }
    else{
        // Do some stuff
    }
}
Acatalectic answered 3/6, 2014 at 11:46 Comment(8)
That will change the status in all conditions. Not just for the condition you mentioned. What I mean is that even if your condition is not true, it will still return 302Neckerchief
@Neckerchief I am only going to throw this exception in case my condition were true, otherwise I'll do whatever I need. I updated my answer to show you how.Acatalectic
Ok, I see what you are saying! Both solutions will workNeckerchief
how did you setup the url to send as it's a redirect?Karynkaryo
@Karynkaryo I don't understand your question, could you explain it clearer?Acatalectic
When you have a redirect, the Location header has to be sent, how did you do it? (Sorry, it was really bad phrased)Karynkaryo
Check at this, I think it is what you are looking forAcatalectic
Why is HttpStatus.MOVED_TEMPORARILY now deprecated?Bluegrass
N
3

The following code will do:

if (someCondition){
    return new ModelAndView("redirect:" + someUrl)
}
else{
        // Do some stuff
}
Neckerchief answered 3/6, 2014 at 11:20 Comment(1)
I found the way to do it that works perfectly for me. I posted the answerAcatalectic
C
1

org.springframework.web.servlet.view.RedirectView has a few simple parameters. One of the parameters removes the model attributes from the URL.

The previous answers worked for me, but the redirect Location header was including the model attributes added by some interceptor post methods.

return new RedirectView(newLocationVia302, true, true, false);
Chalybeate answered 6/5, 2016 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.