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

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. Commented Jun 3, 2014 at 11:26
  • Sorry about it, I have changed the "error" word for status code Commented Jun 3, 2014 at 11:40
  • I found the way to do it that works perfectly for me. I posted the answer Commented Jun 3, 2014 at 11:48

3 Answers 3

6

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

https://stackoverflow.com/a/2067043/2982518

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
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

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 302
@geoand 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.
Ok, I see what you are saying! Both solutions will work
how did you setup the url to send as it's a redirect?
@Panthro I don't understand your question, could you explain it clearer?
|
3

The following code will do:

if (someCondition){
    return new ModelAndView("redirect:" + someUrl)
}
else{
        // Do some stuff
}

1 Comment

I found the way to do it that works perfectly for me. I posted the answer
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.