一 控制器
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 {
// @RequestBody Book book会将传递的xml数据自动绑定到Book对象
@PostMapping(value="/sendxml")
public void sendxml(@RequestBody Book book) {
System.out.println(book);
System.out.println("接收XML数据成功");
}
// @ResponseBody 会将Book自动转成XML数据返回
@PostMapping(value="/readxml")
@ResponseBody
public Book readXml()throws Exception {
// 通过JAXBContext的newInstance方法,传递一个class就可以获得一个上下文
JAXBContext context = JAXBContext.newInstance(Book.class);
// 创建一个Unmarshall对象
Unmarshaller unmar = context.createUnmarshaller();
InputStream is = this.getClass().getResourceAsStream("/book.xml");
// Unmarshal对象的unmarshal方法可以进行xml到Java对象的转换
Book book = (Book) unmar.unmarshal(is);
System.out.println(book);
return book;
}
}
二 领域模型
package org.fkit.domain;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
// @XmlRootElement表示XML文档的根元素
@XmlRootElement
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String author;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(Integer id, String name, String author) {
super();
this.id = id;
this.name = name;
this.author = author;
}
public Integer getId() {
return id;
}
// 该属性作为xml的element
@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 + "]";
}
}
三 视图
sendxml.jsp
<%@ 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>疯狂Java讲义</name><author>李刚</author></book>";
$.ajax("${pageContext.request.contextPath}/sendxml",// 发送请求的URL字符串。
{
type : "POST", // 请求方式 POST或GET
contentType:"application/xml", // 发送信息至服务器时的内容编码类型
// 发送到服务器的数据。
data: xmlData,
async: true , // 默认设置下,所有请求均为异步请求。如果设置为false,则发送同步请求
});
}
</script>
</head>
<body>
</body>
</html>
readxml.jsp
<%@ 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",// 发送请求的URL字符串。
{
dataType : "text", // 预期服务器返回的数据类型。
type : "POST", // 请求方式 POST或GET
async: true , // 默认设置下,所有请求均为异步请求。如果设置为false,则发送同步请求
// 请求成功后的回调函数。
success :function(xml){
// 获得xml数据的id,name,author
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>
四 配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
<context:component-scan base-package="org.fkit.controller"/>
<!-- 默认配置方案 -->
<mvc:annotation-driven/>
<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>
<!-- 视图解析器 p:prefix属性表示前缀 p:suffix 表示后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/content/" p:suffix=".jsp"/>
</beans>
五 测试