<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="org.twtpush.web"/>
</beans>
pom.xml
dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.4</version>
</dependency>
controller
@Controller
@RequestMapping("/push")
public class PushController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private IPushService pushService;
@RequestMapping(value = "/{a}/push",
method = RequestMethod.GET,
produces = {"application/json;charset=UTF-8"})
@ResponseBody
public Operate pu(@PathVariable("a") String a){
Operate operate = new Operate(true,a,0001);
return operate;
}
}
结果
HTTP Status 406 -
type Status report
message
description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
Apache Tomcat/8.0.36
错误具体参考Http 406错误码描述。
根据spring mvc json的一般经验,有几种可能,一个是没有JSON依赖包,一个是JSON解析配置有问题,但是看你的XML配置和Controller都没大问题。
推测可能是你的Java Bean中没有完整的 setter/getter 方法。
Spring的重要一点就是基于Bean的,所以要按照规范来的。