Content negotiation in Spring MVC

后端 未结 2 919
说谎
说谎 2021-01-06 14:27

I am writing a RESTful web application with Spring 3, and part of my application needs to process the data according to the requested media type.

@RequestMap         


        
2条回答
  •  爱一瞬间的悲伤
    2021-01-06 15:08

    Although skaffman's answer is correct, I found in the latest Spring release (3.1 M2), there is an alternative and better way to do this, using consumes and produces values:

    @RequestMapping(value="/pets", consumes="application/json")
    public void addPet(@RequestBody Pet pet, Model model) {
        // ...
    }
    
    @Controller
    @RequestMapping(value = "/pets/{petId}", produces="application/json")
    @ResponseBody
    public Pet getPet(@PathVariable String petId, Model model) {
        // ...
    }
    

    Please check out more details here: http://blog.springsource.com/2011/06/13/spring-3-1-m2-spring-mvc-enhancements-2/

    Update:

    Here are the official Spring documentation about this:

    http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-consumes

    http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-produces

提交回复
热议问题