示例 接收XML格式的数据
创建一个XmlTest项目,在WebContent目录下创建一个js目录,加入jQuery和json2的js文件。
sendxml.jsp
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
   | <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试接收XML格式的数据</title> <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="js/json2.js"></script> <script type="text/javascript"> 	$(document).ready(function() { 		sendxml(); 	}); 	function sendxml() { 		var xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><book><id>1</id><name>书的名称</name><author>作者</author></book>"; 		$.ajax("${pageContext.request.contextPath}/sendxml", 		{ 			type : "POST",  			contentType : "application/xml",  			 			data : xmlData, 			async : true,  		}); 	} </script> </head> <body> </body> </html>
   | 
 
sendxml.jsp页面代码分析如下:
(1)页面使用jQuery发送JSON数据,在页面的<head>部分,引入了jQuery和json2的js文件
(2)载入页面时调用sendxml函数。
(3)sendxml函数发送异步请求到"sendxml",
ajax方法的contentType选项:contentType:"application/xml"表示发送的内容编码格式为XML; 
ajax方法的data选项表示要发送的数据是XML数据。 
Book.java
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60
   | package org.fkit.domain;
  import java.io.Serializable; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement;
 
  @XmlRootElement public class Book implements Serializable { 	private static final long serialVersionUID = 1L; 	private Integer id; 	private String name; 	private String author; 	public Book() 	{ 		super(); 	} 	public Book(Integer id, String name, String author) 	{ 		super(); 		this.id = id; 		this.name = name; 		this.author = author; 	} 	public Integer getId() 	{ 		return id; 	} 	 	@XmlElement 	public void setId(Integer id) 	{ 		this.id = id; 	}
  	public String getName() 	{ 		return name; 	} 	@XmlElement 	public void setName(String name) 	{ 		this.name = name; 	} 	public String getAuthor() 	{ 		return author; 	} 	@XmlElement 	public void setAuthor(String author) 	{ 		this.author = author; 	} 	@Override 	public String toString() 	{ 		return "Book [id=" + id + ", name=" + name + ", author=" + author + "]"; 	} }
   | 
 
在Book类中定义了3个属性:id、nane和author,分别对应XML的元素。 toString方法用来输出获取的数据对象信息。
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
   | package org.fkit.controller;
  import java.io.InputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import org.fkit.domain.Book; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody;
  @Controller public class BookController { 	 	@PostMapping(value = "/sendxml") 	public void sendxml(@RequestBody Book book) 	{ 		System.out.println("接收XML数据成功"); 		System.out.println(book); 	} 	 	@PostMapping(value = "/readxml") 	 	@ResponseBody 	public Book readXml() throws Exception 	{ 		 		JAXBContext context = JAXBContext.newInstance(Book.class); 		 		Unmarshaller unmar = context.createUnmarshaller(); 		 		InputStream is = this.getClass().getResourceAsStream("/book.xml"); 		 		Book book = (Book) unmar.unmarshal(is); 		System.out.println(book); 		return book; 	} }
   | 
 
sendxml方法中的第一个参数@RequestBody Book book表示,使用@RequestBody注解获取到XML数据后,将XML数据设置到Book对象的对应属性中。为了测试接收数据,将接收到XML数据的Book对象打印在控制台上。
springmvc-config.xml文件和JsonRequestTest项目的一致,重点在于<mvc:annotation-driven />,该配置默认装配了Jaxb2RootElementHttpMessageConverter来处理XML数据的转换。
部署XmlTest这个Web应用,在浏览器中输入如下URL来测试应用:
1
   | http://localhost:8080/XmlTest/sendxml.jsp
   | 
 
载λ sendxml.jsp页面时会发送Ajax请求,传递XML数据。BookController接收到请求后,@RequestBody注解会将XML数据设置到Book参数对应的属性中。控制台输出如下:
1 2
   | 接收XML数据成功 Book [id=1, name=书的名称, author=作者]
   | 
 
可以看到,XML数据传递的id、name、 author元素被赋值到了Book对象对应的属性当中。
示例 返回XML格式的数据
readxml.jsp
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 47 48 49
   | <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>测试返回XML格式的数据</title> <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="js/json2.js"></script> <script type="text/javascript"> 	$(document).ready(function() { 		readxml(); 	}); 	function readxml() { 		$.ajax("${pageContext.request.contextPath}/readxml", 		{ 			dataType : "text",  			type : "POST",  			async : true,  			 			success : function(xml) { 				 				var id = $("id", xml).text(); 				var name = $("name", xml).text(); 				var author = $("author", xml).text(); 				var tr = $("<tr align='center'/>"); 				$("<td/>").html(id).appendTo(tr); 				$("<td/>").html(name).appendTo(tr); 				$("<td/>").html(author).appendTo(tr); 				$("#booktable").append(tr); 			}, 			 			error : function() { 				alert("数据接收失败"); 			} 		}); 	} </script> </head> <body>     <table id="booktable" border="1" style="border-collapse: collapse;">         <tr align="center">             <th>编号</th>             <th>书名</th>             <th>作者</th>         </tr>     </table> </body> </html>
   | 
 
readxml.jsp页面代码分析如下:
(1)页面使用jQuery发送M数据,在页面的<head>部分,引入了jQuery和json2的js文件。
(2)载入页面时调用readxml函数。
(3) readxml函数发送异步请求到readxml,请求成功将返回一个XML数据,接到返回的数据后将XML数据中的元素读取出来并将其设置到页面的<span>中。
BookController的readxml方法使用JAXB读取一个XML文件的数据并生成一个Book对象返回。@ResponseBody会将Book对象转换成XML数据返回到前台JSP页面。
在浏览器中输入如下URL来测试应用:
1
   | http://localhost:8080/XmlTest/readxml.jsp
   | 
 
浏览器显示内容如下:
这表示Spring MVC成功将XML数据返回到客户端。
原文链接: 示例 接收XML格式的数据