本文主要是介绍Spring MVC Controller 实现返回XML格式的数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、场景
对于Spring框架项目中,有些情况我们需要返回xml格式数据。这里使用JAXB来实现,而JAXB且已经包含在JDK1.6中了。
二、环境
- Spring 4.0.5.RELEASE
- JDK 1.7
- Eclipse
- Maven
- tomcat7
三、创建项目
(1)创建maven的Web项目
(2)pom.xml文件依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>spring.mvc.xml</groupId><artifactId>spring-mvc-xml</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>spring-mvc-xml Maven Webapp</name><url>http://maven.apache.org</url><properties><spring.version>4.0.5.RELEASE</spring.version></properties><dependencies><!-- Spring 4 dependencies --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency></dependencies><build><finalName>spring-mvc-xml</finalName></build>
</project>
(3)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-appxmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"metadata-complete="true"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-context.xml</param-value></context-param><filter><filter-name>encodingFilter</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>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- spring请求配置,指向springmvc的核心配置文件,定义所有以.do结尾的请求都被springmvc拦截 --><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:spring-context-mvc.xml</param-value></init-param><!--加载顺序为1 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>
(4)spring-context.xml配置
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"></beans>
(5)spring-context-mvc.xml配置
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><mvc:annotation-driven /><!-- 自动扫描controller包下的所有类,如果@Controller注入为bean --><context:component-scan base-package="com.controller" />
</beans>
(6)controller
package com.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import com.bean.User;@Controller
public class UserController {@ResponseBody@RequestMapping("/xml")public User demo() {User user = new User();user.setName("name");user.setPassword("password");return user;}
}
(7)实体类
package com.bean;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement
public class User {private String name;private String password;public User() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
四、测试
(1)启动项目
(2)浏览器访问
http://localhost:8080/xml.do
五、JAXB Annotation说明
@XmlType,将Java类或枚举类型映射到XML模式类型
@XmlAccessorType(XmlAccessType.FIELD) ,控制字段或属性的序列化。FIELD表示JAXB将自动绑定Java类中的每个非静态的(static)、非瞬态的(由@XmlTransient标注)字段到XML。其他值还有XmlAccessType.PROPERTY和XmlAccessType.NONE。
@XmlAccessorOrder,控制JAXB 绑定类中属性和字段的排序。
@XmlJavaTypeAdapter,使用定制的适配器(即扩展抽象类XmlAdapter并覆盖marshal()和unmarshal()方法),以序列化Java类为XML。
@XmlElementWrapper ,对于数组或集合(即包含多个元素的成员变量),生成一个包装该数组或集合的XML元素(称为包装器)。
@XmlRootElement,将Java类或枚举类型映射到XML元素。
@XmlElement,将Java类的一个属性映射到与属性同名的一个XML元素。
@XmlAttribute,将Java类的一个属性映射到与属性同名的一个XML属性。
六、Demo下载
demo地址 : https://github.com/2014team/spring-xml-demo.git
参考 文章:https://blog.csdn.net/Pnoter/article/details/52218363
七、其他方式
引入jar
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream --><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.4.8</version></dependency>
@RequestMapping(value="/jar/xml.do", produces={"application/xml; charset=UTF-8"})@ResponseBodypublic String JarXml() {User2 user = new User2();user.setName("name");user.setPassword("password");XStream xStream = new XStream();xStream.alias("user", User2.class);String xml = xStream.toXML(user);return xml;}
这篇关于Spring MVC Controller 实现返回XML格式的数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!