相关疑难解决方法(0)

如何在没有Accept标头的Spring MVC中设置默认内容类型?

如果在没有Accept标头的情况下将请求发送到我的API,我想将JSON设置为默认格式.我的控制器中有两个方法,一个用于XML,另一个用于JSON:

@RequestMapping(method = RequestMethod.GET,produces=MediaType.APPLICATION_ATOM_XML_VALUE)
@ResponseBody
public ResponseEntity<SearchResultResource> getXmlData(final HttpServletRequest request) {
     //get data, set XML content type in header.
 }

 @RequestMapping(method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
 @ResponseBody
 public ResponseEntity<Feed> getJsonData(final HttpServletRequest request){
      //get data, set JSON content type in header.  
 }
Run Code Online (Sandbox Code Playgroud)

当我发送没有Accept标头的请求时,getXmlData会调用该方法,这不是我想要的.getJsonData如果没有提供Accept标头,有没有办法告诉Spring MVC调用该方法?

编辑:

有一个defaultContentType领域ContentNegotiationManagerFactoryBean可以解决问题.

rest http spring-mvc content-negotiation

26
推荐指数
2
解决办法
3万
查看次数

标签 统计

content-negotiation ×1

http ×1

rest ×1

spring-mvc ×1