帮助 - 我还能做些什么来修复这个错误? 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>
这是我的 Controller
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();
}
}
这里是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
}
}
最佳答案
在您的 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>
并且您的 Controller 注释为:
@RestController
@RequestMapping( "user" )
public class UserController {
...
}
您的 Controller 类根本不会加载到容器中。顺便说一句,根据您的异常消息:
I am getting the error
org.springframework.web.HttpMediaTypeNotAcceptableException:: Could not find acceptable representation.
如果您没有在上下文文件中注册以下内容,则会引发此异常:
-
<mvc:annotation-driven />
(您尚未在上下文文件中注册) - 如果需要的 JAR 不在类路径中,(您已经有了)
- 使用 ENUM 时,用
@JsonFormat(shape= JsonFormat.Shape.OBJECT)
注释您的 ENUM 类
编辑: 使用 Spring 4.0.1.RELEASE 和以下单个 JSON 依赖项测试对我来说工作正常:
com.fasterxml.jackson.core jackson 数据绑定(bind) 2.4.3
和 Controller 类:
@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>
关于java - HttpMediaTypeNotAcceptableException : Could not find acceptable representation spring mvc 4. 1.5 与 com.fasterxml 2.5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28912462/