java - Spring 错误: only capable of generating responses with characteristics not acceptable according to the request "accept" headers

标签 java spring maven spring-mvc

我使用 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 错误:

enter image description here

我确保我已经添加了

<mvc:annotation-driven />

在我的 spring-config.xml 中。 enter image description here

我也确定我在 pom.xml 中添加了这些 jackson 依赖项

enter image description here

********************编辑**************************** **********

enter image description here

********************再次编辑**************************** ******

如您所见,我没有限制 @RequestMapping 注释中的 header ,因此我认为这不是与 header 限制相关的问题。

此外,我的网址模式如下:

enter image description here enter image description here

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/

相关文章:

jvm - 在没有 JVM 的机器上运行 Java 应用程序

java - 使用@Validated验证序列和短路

maven - 添加浏览器驱动程序作为 Maven 依赖项的原因是什么?

Android Studio 添加基于 maven 的项目 libas 依赖项

java - 如何将jar文件转换为war并部署到tomcat服务器中

java - 制作节 :s component lookup mechanism wait for a component to exist

java - 为什么 IllegalArgumentException (JDK 1.4.2) 不能用可抛出的原因构造?

java - 扫描仪帮助 - Java

java - 如何使用 MySQL Read-Replica 配置 Spring/Hibernate

java - Sprint 事务 - 如果内部事务失败,则需要 REQUIRES_NEW 行为