Spring MVC提供了处理XML
格式请求响的 HttpMessageConverter
,只需在 Spring Web
容器中为RequestMappingHandlerAdapter
装配处理XML
的HttpMessageConverter
,并在交互过程中通过请求Accept
指定MIME
类型,Sping MVC就可以使服务端的处理方法和客户端XML格式的消息进行通信。开发者无须关心通信层的数据格式,可以将精力集中到业务处理上。
Spring MVC默认使用Jaxb2RootElementHttpMessageConverter
转换XML
格式的数据,JAXB
(Java Architecture for XML Binding)可以很方便地生成XML ,也能很方便地生成JSON
,这样可以很方便地在XML 和 JSON 之间进行转换。
JAXB 是一个业界标准,是一项可以根据XML Schema 产生Java 类的技术。在该过程中,JAXB 提供了将XML 文档反向生成Java 对象的方法,并能将Java 对象内容重新写到XML 实例文档中,从而使得 Java 开发者在 Java 应用中能够很方便地处理XML数据。
JAXB 常用的注解包括:@XmlRootElement, @XmlElement
等。
springmvc.xml
文件添加注解驱动<mvc:annotation-driven />
,该配置默认装配了Jaxb2RootElementHttpMessageConverter
来处理XML
数据的转换。
接收XML格式数据
前端发送Ajax请求,data是xml数据,contentType指定发送的数据格式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <script type="text/javascript" > function sendXMLData ( ){ var xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone = \"yes\" ?><user><userName>admin</userName><password>admin112233</password><age>33</age></user>" ; $.ajax ({ url : '${pageContext.request.contextPath}/api/getXMLData' , type : 'post' , data : xmlData, contentType : "application/xml" , success : function (data ){ console .log (data); } }); } </script>
定义之实体类接收XML数据,添加XML注解@XmlRootElement:XML根元素,默认是实体类类名首字母小写;可设置根元素名称,但必须与接收到的XML数据的根元素名一致,如:@XmlRootElement = (name = “user”)。@XmlElement :设置XML内部元素,注解放在 set 方法上。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 @XmlRootElement public class User { private String userName; private String password; private int age; public User () { super (); } public User (String userName, String password, int age) { super (); this .userName = userName; this .password = password; this .age = age; } public String getUserName () { return userName; } @XmlElement public User setUserName (String userName) { this .userName = userName; return this ; } public String getPassword () { return password; } @XmlElement public User setPassword (String password) { this .password = password; return this ; } public int getAge () { return age; } @XmlElement public User setAge (int age) { this .age = age; return this ; } @Override public String toString () { return "User [userName=" + userName + ", password=" + password + ", age=" + age + "]" ; } }
后台Controller接受数据@RequestBody 将请求数据绑定到实体对象中。@ResponseBody 返回的是实体数据,实体类加了XML
注解,则会自动将实体数据转换为XML 数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 @Controller @RequestMapping("/api") public class XMLController { @RequestMapping(value = "/xml") public String returnXMLView () { return "sendxml" ; } @RequestMapping(value = "/getXMLData", method = {RequestMethod.POST}) @ResponseBody public User getXMLData (@RequestBody User user) { user.setAge(88 ); System.out.println(user); return user; } } <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <user> <age>88 </age> <password>admin112233</password> <userName>admin</userName> </user>
返回XML格式数据 从XML 文件读取数据绑定到实体类,将该实体类转换成XML 格式返回给前端。
前端jsp和ajax请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <html > <head > <meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" > <title > Insert title here</title > <script type ="text/javascript" src ="../static/js/jquery-1.11.3.js" > </script > <script type ="text/javascript" src ="../static/js/json2.js" > </script > <script type ="text/javascript" > $(document ).ready (function ( ){ getXMLData (); }); function getXMLData ( ){ $.ajax ({ url : '${pageContext.request.contextPath}/api/reSTXMLData' , type : 'post' , dataType : 'text' , success : function (data ){ alert (data); var name = $("name" , data).text (); var age = $("age" , data).text (); var address = $("address" , data).text (); $('#nameId' ).html (name); $('#ageId' ).html (age); $('#addressId' ).html (address); console .log (data); alert (JSON .stringify (data)); } }); } </script > </head > <body > <h3 > SendXML</h3 > name: <span id ="nameId" > </span > <br > age: <span id ="ageId" > </span > <br > address: <span id ="addressId" > </span > </body > </html >
创建XML 文档,填入数据,文档放到项目的src
根目录。
1 2 3 4 5 6 <?xml version="1.0" encoding="UTF-8" ?> <student > <name > 李小虎</name > <age > 15</age > <address > 广东广州</address > </student >
创建实体类并添加xml
注解
1 2 3 4 5 6 7 8 9 10 11 @XmlRootElement public class Student { private String name; private int age; private String address; ....set/get方法.... }
后台Controller层处理方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 @Controller @RequestMapping("/api") public class XMLController { @RequestMapping(value = "/xml") public String returnXMLView () { return "sendxml" ; } @RequestMapping(value = "/reSTXMLData", method = {RequestMethod.POST}) @ResponseBody public Student reSTXMLData () throws JAXBException { JAXBContext ctx = JAXBContext.newInstance(Student.class); Unmarshaller unmar = ctx.createUnmarshaller(); InputStream inputStream = this .getClass().getResourceAsStream("/student.xml" ); Student st = (Student) unmar.unmarshal(inputStream); return st; } }