我使用 spring 构建 Restful API,当我访问以下方法时:
// get the entity in DB by using id number
@RequestMapping(value = "/{id:.+}", method = RequestMethod.GET)
public @ResponseBody
User getEmployee(@PathVariable("id") String email) {
User user=null;
System.out.println(email);
try {
user = dataServices.getEntityById(email);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(user.getNickname());
return user;
}
使用此 URL:http://localhost:8080/RestCrud/user/richard_johnson@sina.com
出现 406 错误:
我确保我已经添加了
<mvc:annotation-driven />
我也确定我在 pom.xml 中添加了这些 jackson 依赖项
********************编辑**************************** **********
********************再次编辑**************************** ******
如您所见,我没有限制 @RequestMapping 注释中的 header ,因此我认为这不是与 header 限制相关的问题。
此外,我的网址模式如下:
http://localhost:8080/RestCrud/user/id
我已经测试了“列表” http://localhost:8080/RestCrud/user/list
它可以工作,但“id”路径不起作用
最佳答案
看来你已经注释掉了一些 Jackson 依赖项 发生错误的原因是您的员工对象无法转换为浏览器可接受的格式。你肯定会想用 json 进行响应。
Spring 4 需要以下 jackson 库
- jackson 核心
- jackson 核心萨尔
- jackson 映射器-asl
- jackson 数据绑定(bind)
更新:
查看您的 URL 模式,*.com 扩展正在插入 spring 执行内容协商,而不是验证接受 header 。
您可以使用
强制 spring 不进行基于路径扩展的内容协商<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
</bean>
关于java - Spring 错误: only capable of generating responses with characteristics not acceptable according to the request "accept" headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31983305/