Spring MVC @RequestMapping headers can accept only one value?

前端 未结 2 1808
渐次进展
渐次进展 2020-12-08 08:03

This will work:

@RequestMapping(value = \"/test\", method = RequestMethod.POST,
    headers = {\"content-type=application/json\"}) {
    .......
}

相关标签:
2条回答
  • 2020-12-08 08:27

    Have you tried doing content-type=application/json,application/xml?

    Not sure if it would work but putting two content-type headers in there I think only one will win.

    OR

    possibily use two RequestMapping annotations on the same method with different content-type headers?

    0 讨论(0)
  • 2020-12-08 08:47

    If you are using Spring 3.1.x. You can look at using consumes, produces attributes of @RequestMapping annotation. Here is the Spring blog post on the improvements:

    http://spring.io/blog/2011/06/13/spring-3-1-m2-spring-mvc-enhancements/

    Snippet from the doc above:

    @RequestMapping(value="/pets", headers="Content-Type=application/json")
    public void addPet(@RequestBody Pet pet, Model model) {
        // ...
    }
    

    is replaced by:

    @RequestMapping(value="/pets", consumes="application/json")
    public void addPet(@RequestBody Pet pet, Model model) {
        // ...
    }
    

    In addition, if you need multiple media types. You can do the following:

    produces={"application/json", "application/xml"}
    
    consumes={"application/json", "application/xml"}
    
    0 讨论(0)
提交回复
热议问题