关于spring4.2.1 @RestController 406的问题

java干货 2015-09-24 09:41:24
疯啦,疯啦,疯啦,疯啦,
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springjdbc</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.txt</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--<welcome-file-list>-->
<!--<welcome-file>/WEB-INF/page/prelogin.jsp</welcome-file>-->
<!--</welcome-file-list>-->
</web-app>

applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 加载类路径(classpath:)下的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 开启注解,采用注解的方式 -->
<context:annotation-config />

<!-- 扫描 com.merryyou包下的带注解的组件 -->
<context:component-scan base-package="com.merryyou" />

<!-- 配置spring自带的数据源数据库 -->
<!--<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/userdb" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>-->

<!--配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.main.driverClass}" />
<property name="jdbcUrl" value="${jdbc.main.jdbcUrl}?zeroDateTimeBehavior=convertToNull" />
<property name="user" value="${jdbc.main.user}" />
<property name="password" value="${jdbc.main.password}" />
<property name="minPoolSize" value="${jdbc.main.minPoolSize}" />
<property name="maxPoolSize" value="${jdbc.main.maxPoolSize}" />
<property name="initialPoolSize" value="${jdbc.main.initialPoolSize}" />
<property name="acquireIncrement" value="${jdbc.main.acquireIncrement}" />
<property name="acquireRetryAttempts" value="${jdbc.main.acquireRetryAttempts}" />
</bean>
<!-- 配置事物管理器(基于注解) -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 开启事物注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>


spring-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- 扫描 com.merryyou包下的带注解的组件 -->
<context:component-scan base-package="com.merryyou"/>

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

<!--<context:annotation-config />-->
<!--<mvc:annotation-driven />-->
<mvc:annotation-driven content-negotiation-manager="contentManager"/>
<bean id="contentManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" />
<property name="useJaf" value="false"/>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="html" value="text/html" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

AuthUserController
package com.merryyou.controller.api;

import com.merryyou.domain.User;
import com.merryyou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* Created on 2015/9/24 0024.
*
* @author zhenglongfei
* @since 1.0
*/
@RestController
@RequestMapping("/authUser")
public class AuthUserController {

@Autowired
private UserService userService;

@RequestMapping(value = "/list",method = RequestMethod.GET,produces={ "application/xml","application/json"})
public List list() throws Exception{
List<User> userList = userService.getUserList();
System.out.print("*****************");
return userList;
}
}


访问页面显示
...全文
229 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bartholomew4 2015-09-25
  • 打赏
  • 举报
回复
楼上正解,记得除了modleandview其他都要用responsebody
java干货 2015-09-24
  • 打赏
  • 举报
回复
来大神解救我啊!
tony4geek 2015-09-24
  • 打赏
  • 举报
回复
刚写了个测试时可以的。
@RequestMapping(value = { "/User/{id}", "/user/{id}.json" }, method = RequestMethod.GET,produces={ "application/xml","application/json"})
	public @ResponseBody User getUser(@PathVariable String id, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("getUser-------------->id=" + id);
		User u = userService.getUser(1);
		return u;
	}
@ResponseBody
scmod 2015-09-24
  • 打赏
  • 举报
回复
看下请求头的accept能不能接受响应是你定义的那块json类型的 没有的话应该是你的请求有问题,像ajax这种能指定数据类型是json的应该就能接收这个类型的响应 @RestController我也没用过~
  • 打赏
  • 举报
回复
把@RestController 改成@Controller哦


欢迎访问: http://lvvbbe.com/

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧