我有以下简单的Spring MVC测试方法来使用ResponseEntity进行测试。
@RequestMapping(value = "/", method = RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, String>> home(Locale locale, Model model) {
Map<String,String> returnMap = new HashMap<String,String>(2);
returnMap.put("lang1", "Java");
returnMap.put("lang2", "C++");
ResponseEntity<Map<String, String>> respEntity = new ResponseEntity<Map<String,String>>(returnMap, HttpStatus.OK);
return respEntity;
}
创建了ResponseEntity,在Eclipse调试器中,我可以看到ResponseEntity对象的正文,标题和状态代码都是正确的,但在页面中我收到以下错误:
The resource identified by this request is only capable of generating
responses with characteristics not acceptable according to the request
"accept" headers ().
如果有人能告诉我我做错了什么,我真的很感激。
答案 0 :(得分:0)
Remove生成JSON,或者将responsebody注释添加到返回类型并更改返回类型;将ResponseEntity作为返回类型并将方法注释为生成JSON不是goign工作。一个或另一个。
实际上考虑一下,spring / jackson实际上可能会自动将响应能力转换为json。但无论哪种方式,你都需要响应来生成json。
答案 1 :(得分:0)
您是否尝试过使用以下RequestMapping:
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
答案 2 :(得分:0)
当我遇到此问题时,因为我的mvc-config.xml
文件中缺少以下内容:
<mvc:annotation-driven />