SpringMVC 4.3.4 返回JSON数据

来源:互联网 发布:网络侦探 编辑:程序博客网 时间:2024/06/09 17:32
使用Maven管理的依赖库,

在pom.xml的 dependencies 里面配置Spring和jackson的依赖,配置后,会自动加载,spring context core aop 等其他spring依赖包添加 jackson-databind 后,jackson-core, jackson-annotations也会自动依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.4.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.8.5</version></dependency>

保存,更新Maven,自动加载依赖库
创建MvcController,内容如下

@Controller@RequestMapping("/mvc")public class MvcController {@RequestMapping(value="/json", method=RequestMethod.GET)public @ResponseBody User getUserJson() {User user = new User();user.setUsername("用户名");user.setPassword("密码");return user;}}

web.xml内容如下

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>Tx-advice</display-name>  <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>  <servlet>  <servlet-name>springMVC</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:springmvc-servlet.xml</param-value>  </init-param>  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>springMVC</servlet-name>  <url-pattern>/</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util" 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/aop     http://www.springframework.org/schema/aop/spring-aop.xsd    http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx.xsd    http://www.springframework.org/schema/util    http://www.springframework.org/schema/util/spring-util.xsd    http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc.xsd">   <context:component-scan base-package="com.ing.po"></context:component-scan></beans>

springmvc-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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util" 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/aop     http://www.springframework.org/schema/aop/spring-aop.xsd    http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx.xsd    http://www.springframework.org/schema/util    http://www.springframework.org/schema/util/spring-util.xsd    http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.ing.controller"></context:component-scan><mvc:default-servlet-handler /><bean id="internalResourceViewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"></property><property name="suffix" value=".jsp"></property></bean><mvc:annotation-driven></mvc:annotation-driven></beans>

配置完成之后,启动项目,访问http://localhost:8080/webdemo/mvc/json,正确返回json数据。



如果想返回XML数据,在实体类上面使用 @XmlRootElement 注解即可,贴出实体类User的源码

package com.ing.po;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class User {private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

再访问原来返回json数据的网址,返回的就是XML格式的数据了


总结:其实Spring4.0之后的版本返回json数据的配置很简单,只要加入jackson的依赖库,在controller里面的映射器方法前使用@ResponseBody注解即可。
如果想返回xml数据,不用添加其他的jar包,只需要在实体类加入 @XmlRootElement 注解类,在controller里面的映射器方法前使用@ResponseBody注解即可。


0 0
原创粉丝点击