3

In Spring MVC @RequestMapping annotation, I am returning JSP page name as result. This returns HTTP status code 200 OK. How can I change this status code to something like 201 created ?

@ResponseStatus doesn't work. Also, HttpServletResponse won't work since I need to return my custom JSP page only.

@RequestMapping(method = RequestMethod.POST)
public String addPhone(@ModelAttribute("phone") Phone phoneVO) {
                phoneManager.addPhone(phoneVO);
                return "redirect:/phone";
}
Shubham Vadhera
  • 108
  • 1
  • 3
  • 8

4 Answers4

7

For those of you that wants to set a status returning a model object like me:

@RequestMapping(value = "/yourRoute", method = RequestMethod.POST)
public ModelAndView accountsPagePOST(@RequestBody final String body) 
{
    ModelAndView model = new ModelAndView("yourView");
    //TODO do your logic and add your objects
    model.setStatus(HttpStatus.OK);
    return model;
}

Hope this helps someone... You can obviously set a failed status in case something goes wrong.

Ph0b0x
  • 664
  • 7
  • 20
3

You should try doing following, you should return your view name instead without any redirect, the spring view resolver should do the needful to resolve your custom jsp. (You should configure view resolver properly)

 @RequestMapping(method = RequestMethod.POST)
 public String addPhone(@ModelAttribute("phone") Phone phoneVO,    HttpServletResponse response) {
            phoneManager.addPhone(phoneVO);
            response.setStatus(HttpServletResponse.SC_CREATED);
            return "phone";
 }

Other option could be using @ResponseStatus annotation on your handler method itself as it is certain that the responsibility of addPhone is to create a new resource on server. Hence you could define on handler method the status.

 @RequestMapping(method = RequestMethod.POST)
 @ResponseStatus(HttpStatus.CREATED)
 public String addPhone(@ModelAttribute("phone") Phone phoneVO,    HttpServletResponse response) {
            phoneManager.addPhone(phoneVO);
            return "phone";
 }
ScanQR
  • 3,740
  • 1
  • 13
  • 30
  • phone is actually the url (/phone) and not the view name. I need to redirect only because simply calling the view directly doesn't allow me to pass parameters. – Shubham Vadhera Nov 16 '16 at 18:27
  • @ShubhamVadhera what parameters you are referring to in your question? – ScanQR Nov 16 '16 at 18:32
  • Any attribute from program to the jsp page via `model.addAttribute`. For example: `model.addAttribute("nextid", phoneManager.getNextID());` – Shubham Vadhera Nov 16 '16 at 18:42
0

Ok, I found the solution:

response.setStatus(HttpServletResponse.SC_CREATED); return "phonePage";

As mentioned by @SotiriosDelimanolis, the redirect was overwriting the value in setStatus. So, instead of redirect, I am calling the JSP page directly (while also re-sending the parameters).

I guess with redirect, the status has to be HTTP OK.

Shubham Vadhera
  • 108
  • 1
  • 3
  • 8
-2
@RequestMapping(method = RequestMethod.POST)
public String addPhone(@ModelAttribute("phone") Phone phoneVO) {
    phoneManager.addPhone(phoneVO);
    return "/phone";
}

Oops, sent before I was finished. Intended to write that you can add HttpServletResponse as a parameter and then use it to set the code.

mattsemar
  • 1
  • 1
  • That `sendRedirect` is going to overwrite the value in `setStatus`. There are nicer ways to achieve redirects without having to involved the Servlet API in your controller type. – Sotirios Delimanolis Nov 16 '16 at 04:11
  • Now I have no idea what you're suggesting. – Sotirios Delimanolis Nov 16 '16 at 04:27
  • As mentioned in question, I cannot use HttpServletResponse since I need to respond with a custome JSP page only. Looks like the redirect is overwriting the response status. Need to find another way to redirect. – Shubham Vadhera Nov 16 '16 at 18:30