HttpMediaTypeNotAcceptableException:找不到可接受的表示spring mvc 4.1.5 with com.fasterxml 2.5.1

时间:2015-03-07 07:19:43

标签: java json spring spring-mvc jackson

帮助 - 我还能做些什么来解决此错误? org.springframework.web.HttpMediaTypeNotAcceptableException ::找不到可接受的表示。

我认为我的项目已正确配置以处理json restful请求。在过去的几天里,我已阅读并应用各种建议无济于事。关于我应该采取哪些不同的做法还有其他想法吗?

我正在使用Spring MVC 4.1.5和com.fasterxml 2.5.1。

我的pom.xml的一部分

 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.5.1</version>
    <type>bundle</type>
 </dependency>
 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.5.1</version>
    <type>bundle</type>
 </dependency>
 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.1</version>
    <type>bundle</type>
 </dependency>

这是我的web.xml的一部分

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/appServlet/servlet-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

这是我的servlet-context.xml的一部分

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.losgatos">
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>   

这是我的控制器

package com.losgatos.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.losgatos.User;

@RestController
@RequestMapping( "user" )
public class UserController { 

    @RequestMapping( value = "data", produces="application/json" )
    public User getUser(){
        return new User();
    }   
}

这是用户用户POJO

package com.losgatos;

import java.util.EnumSet;

public class User {

    public User(){
        id = 0;
        age = 22;
        name = "Titus Feng"; 
        alias = "tornado tie";
        roles = EnumSet.of( Role.NORMAL, Role.NEW );
    }

    private int id, age;
    private String name, alias;
    private EnumSet<Role> roles;

    //added getters and setters here

    public enum Role{
        NEW, NORMAL, ADMIN, MEMBER, DORMANT 
    }   
}

3 个答案:

答案 0 :(得分:2)

在Spring上下文配置文件(dispatcher-servlet.xml)中,您有:

<context:component-scan base-package="com.losgatos">
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>

并且您的控制器注释为:

@RestController
@RequestMapping( "user" )
public class UserController { 
...
}

您的控制器类根本不会加载到容器中。 BTW根据您的异常消息:

  

我收到错误

     

org.springframework.web.HttpMediaTypeNotAcceptableException :: Could   找不到可接受的代表。

如果您未在上下文文件中注册以下内容,则会引发此异常:

  • <mvc:annotation-driven />(您尚未在上下文文件中注册)
  • 如果需要,JAR在类路径中不存在,(您已经拥有)
  • 使用ENUM时,使用@JsonFormat(shape= JsonFormat.Shape.OBJECT)
  • 注释您的ENUM课程

编辑: 测试工作正常,使用Spring 4.0.1.RELEASE和以下单个JSON依赖:

    com.fasterxml.jackson.core     杰克逊 - 数据绑定     2.4.3

和控制器类:

@RestController
@RequestMapping("/u")
public class UserRestController {

    @RequestMapping( value = "/data", produces="application/json" )
    public RestUser getUser(){
        return new RestUser();
    } 
}

得到:

{"id":0,"age":22,"name":"Titus Feng","alias":"tornado tie","roles":["NEW","NORMAL"]}

建议, 改变这个:

<context:component-scan base-package="com.losgatos">
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>

为:

<context:component-scan base-package="com.losgatos">
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" />
</context:component-scan>

答案 1 :(得分:1)

Spring Framework 4.1 raised some dependencies requirements;您需要将Jackson升级到2.3或更高版本。请注意,工件groupId已更改为com.fasterxml

答案 2 :(得分:1)

我最终通过以下方式工作:
从我的fasterxml.jackson pom.xml条目中删除<type>bundle</type> 重新运行我的pom.xml文件
清理项目构建(项目 - &gt;清洁)
清洁并重新启动服务器