SpringMVC 使用@ResponseBody返回json
2019-03-01 本文已影响0人
煎包小混沌
1:关于web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--使用ContextLoaderListener启动spring配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--启动servlet-->
<servlet>
<servlet-name>dispacth</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--servlet映射-->
<servlet-mapping>
<servlet-name>dispacth</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2:关于dispacth-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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.smart.web"/>
<!--一定要加,否则在使用@ResponseBody注解会异常-->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
3:顺便记录下applicationContext.xml配置,使用了Hibernate
<?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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--自动扫描注解-->
<context:component-scan base-package="com.smart.dao"/>
<!--jdbc操作数据库-->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="jdbcDataSource"/>
<!--jdbc处理图片lob,text等类型-->
<bean id="lobHandler"
class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="true"/>
<!--定义一个jdbc数据源,创建一个驱动管理数据源的bean -->
<bean id="jdbcDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/myTest" />
<property name="username" value="root" />
<property name="password" value="wfx123456" />
</bean>
<!--hibernate-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
p:dataSource-ref="jdbcDataSource"
p:packagesToScan="com.smart.dao">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--配置HibernateTemplate Bean 对象操作数据库-->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate5.HibernateTemplate"
p:sessionFactory-ref="sessionFactory"/>
<!--配置Hibernate事务管理器-->
<bean id="transactionManage"
class="org.springframework.orm.hibernate5.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<tx:annotation-driven transaction-manager="transactionManage"/>
</beans>
4:使用mvc返回json,需要依赖以下库,注意库的版本,可能会冲突
<!--使用mvc返回json,需要导入以下库-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version> ${jackson-core.version} </version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version> ${jackson-databind.version} </version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version> ${jackson-annotations.version} </version>
</dependency>
5:一直出现错误
org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.HashMap
将maven的库全部删除,然后重新加入一遍,如图:
image.png
重新配置tomcat
image.png
6:返回json
@RequestMapping(value = "/showUserListByJson")
@ResponseBody
public Map<String, Object> showUserListInJson(ModelMap mm) {
User user = (User) mm.get("user");
user.setUserId(1);
user.setUserName("2");
ArrayList<User> list = new ArrayList<User>();
list.add(user);
Map<String, Object> mapMode = new HashMap<String, Object>();
mapMode.put("userList", list);
mapMode.put("word", "研发");
return mapMode;
}