Spring MVC接受XML格式的数据

来源:互联网 发布:mac系统剪切文件 编辑:程序博客网 时间:2024/05/22 14:46

代码:

sendxml.jsp

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html>  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>测试接收XML格式的数据</title>  
  8. <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>  
  9. <script type="text/javascript" src="js/json2.js"></script>  
  10. <script type="text/javascript">  
  11. $(document).ready(function(){  
  12.     sendxml();  
  13. });  
  14.   
  15. function sendxml(){  
  16.     var xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><book><id>1</id><name>疯狂Java讲义</name><author>李刚</author></book>";  
  17.     $.ajax("${pageContext.request.contextPath}/sendxml",// 发送请求的URL字符串。  
  18.             {  
  19.              type : "POST", //  请求方式 POST或GET  
  20.              contentType:"application/xml", //  发送信息至服务器时的内容编码类型  
  21.              // 发送到服务器的数据。  
  22.              data: xmlData,  
  23.              async:  true , // 默认设置下,所有请求均为异步请求。如果设置为false,则发送同步请求  
  24.     });  
  25. }  
  26. </script>  
  27. </head>  
  28. <body>  
  29.   
  30. </body>  
  31. </html>  

Book.java

[java] view plain copy
  1. package com.bean;  
  2.   
  3. import java.io.Serializable;  
  4. import javax.xml.bind.annotation.XmlElement;  
  5. import javax.xml.bind.annotation.XmlRootElement;  
  6.   
  7. // @XmlRootElement表示XML文档的根元素  
  8. @XmlRootElement  
  9. public class Book implements Serializable {  
  10.     private Integer id;  
  11.     private String name;  
  12.     private String author;  
  13.     public Book() {  
  14.         super();  
  15.         // TODO Auto-generated constructor stub  
  16.     }  
  17.     public Book(Integer id, String name, String author) {  
  18.         super();  
  19.         this.id = id;  
  20.         this.name = name;  
  21.         this.author = author;  
  22.     }  
  23.     public Integer getId() {  
  24.         return id;  
  25.     }  
  26.     // 该属性作为xml的element  
  27.     @XmlElement  
  28.     public void setId(Integer id) {  
  29.         this.id = id;  
  30.     }  
  31.   
  32.     public String getName() {  
  33.         return name;  
  34.     }  
  35.     @XmlElement  
  36.     public void setName(String name) {  
  37.         this.name = name;  
  38.     }  
  39.   
  40.     public String getAuthor() {  
  41.         return author;  
  42.     }  
  43.     @XmlElement  
  44.     public void setAuthor(String author) {  
  45.         this.author = author;  
  46.     }  
  47.   
  48.     @Override  
  49.     public String toString() {  
  50.         return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";  
  51.     }  
  52.   
  53. }  

BookController.java

[java] view plain copy
  1. package com.control;  
  2.   
  3. import org.apache.commons.logging.Log;  
  4. import org.apache.commons.logging.LogFactory;  
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.RequestBody;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RequestMethod;  
  9.   
  10. import com.bean.Book;  
  11.   
  12. @Controller  
  13. public class BookController {  
  14.     private static final Log logger = LogFactory.getLog(BookController.class);  
  15.       
  16.     // @RequestBody Book book会将传递的xml数据自动绑定到Book对象  
  17.     @RequestMapping(value="/sendxml", method=RequestMethod.POST)  
  18.     public void sendxml(@RequestBody Book book){  
  19.         logger.info(book);  
  20.         logger.info("接受XML数据成功!");  
  21.     }  
  22. }  

截图:

0 0
原创粉丝点击