Multiple scenarios @RequestMapping produces JSON/XML together with Accept or ResponseEntity

后端 未结 3 1722
-上瘾入骨i
-上瘾入骨i 2020-12-05 01:51

I am working with Spring 4.0.7

About Spring MVC, for research purposes, I have the following:

@RequestMapping(value=\"/getjsonperson\", 
                    


        
3条回答
  •  生来不讨喜
    2020-12-05 02:43

    All your problems are that you are mixing content type negotiation with parameter passing. They are things at different levels. More specific, for your question 2, you constructed the response header with the media type your want to return. The actual content negotiation is based on the accept media type in your request header, not response header. At the point the execution reaches the implementation of the method getPersonFormat, I am not sure whether the content negotiation has been done or not. Depends on the implementation. If not and you want to make the thing work, you can overwrite the request header accept type with what you want to return.

    return new ResponseEntity<>(PersonFactory.createPerson(), httpHeaders, HttpStatus.OK);

提交回复
热议问题