Announcement Announcement Module
Collapse
No announcement yet.
response status code and head attributes with REST Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • response status code and head attributes with REST

    hi all
    I am trying to implement a RESTful web service following this nice article.

    Everythig is allright but now I need to manage the response message status along with some header parameters such as "Location" to be REST compliant.

    How can I do that?

  • #2
    ok, as usual after you post a question it takes just few time to find the answer.

    I found a workaround which it seems to work, I hope it could help.
    The trick consists of passing HttpServletResponse as an argument in the method mapping the client request.

    here it is the PUT example where I want to return a 201 CREATED if everything is going fine.

    Code:
    	@RequestMapping(method=RequestMethod.PUT, value="/document/{id}")
    	public ModelAndView updateDocument(@RequestBody String body, @PathVariable String id, HttpServletResponse rsp, HttpServletRequest rqst) {
    		Source source = new StreamSource(new StringReader(body));
    		Document d = (Document) jaxb2Marshaller.unmarshal(source);
    		d.setId(Integer.parseInt(id));
    		persistenceService.saveOrUpdate(d, id);
    
    		rsp.setStatus(HttpServletResponse.SC_CREATED);
    		rsp.setHeader("Location", "your URI");
    
    		return new ModelAndView(XML_VIEW_NAME, "object", d);
    	}
    This piece of code doesn't handle errors, of course.

    I am very intersted in knowing if this procedure can be tollerated or if it is just a dirty trick. Is there a nicer way to do this?

    Comment


    • #3
      Could use an annotation

      How about this instead?

      Code:
      	@RequestMapping(method=RequestMethod.PUT, value="/document/{id}")
              @ResponseStatus(HttpStatus.OK)
      	public ModelAndView updateDocument(@RequestBody String body, @PathVariable String id) {
      		Source source = new StreamSource(new StringReader(body));
      		Document d = (Document) jaxb2Marshaller.unmarshal(source);
      		d.setId(Integer.parseInt(id));
      		persistenceService.saveOrUpdate(d, id);
      
      		return new ModelAndView(XML_VIEW_NAME, "object", d);
      	}

      Comment


      • #4
        this is elegant but it seems not suitable if I need to output different HTTP Status messages depending on what is happening behind the scenes.

        Correct me if I mistake something but it seems that the method you write outputs an "ok" if it is capable of returning the ModelAndView. If it can not which status is set in the response? And how can I manage different status codes?

        Comment


        • #5
          exception handlers

          Your controller methods declare the expected response type for the happy path. For various exceptions you can write exception handlers and return different response types as appropriate.


          Code:
          @ExceptionHandler(LadyGagaException.class)
          @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
           public void handleOverratedMediaException(LadyGagaException e) {
          ...

          Comment


          • #6
            very clear. This definetly makes sense. Thank you for your reply.

            Comment

            Working...
            X