需求: 1、springmvc返回xml; 技術及環境: 實現: spirngxml的配置主要如下: 添加項目依賴: 實體類JavaBean 一個簡單的JavaBean,添加了JAXB 註解,spring將會根據請求判斷轉換成xml。JAXB不需要添加額外的依賴庫,已經包含在jdk中。 spring ...
需求:
1、springmvc返回xml;
技術及環境:
- Spring 4.3.1.RELEASE
- JDK 1.8
- IDEA 15.0.6
- Maven 3
實現:
spirngxml的配置主要如下:
添加項目依賴:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.1.RELEASE</version> </dependency>
實體類JavaBean
一個簡單的JavaBean,添加了JAXB 註解,spring將會根據請求判斷轉換成xml。JAXB不需要添加額外的依賴庫,已經包含在jdk中。
BaseXmlResult 的作用是作為返回的標誌和附加消息對象(如:操作成功!操作失敗等)。抽取出來,不用每個返回對象都寫code和message,只需要繼承即可。
XmlActionResult 為具體的要返回的信息的對象。前臺頁面需要解析的數據。該對象中Data為泛型對象,返回時,可根據實際情況,設置Person.class|Man.class|Woman.class對象,若有其他對象,只需對註解進行調整即可,方便維護。
public class BaseXmlResult { protected String code; protected String message; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
@XmlRootElement(name = "xml") @XmlAccessorType(XmlAccessType.NONE) public class XmlActionResult<T> extends BaseXmlResult{ @XmlElements({ @XmlElement(name="Person",type = Person.class), @XmlElement(name="Man",type = Man.class), @XmlElement(name="Woman",type = Woman.class) }) private T data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
@XmlRootElement @XmlAccessorType(XmlAccessType.NONE) public class Person { @XmlElement private String name; @XmlElement private Integer sex; @XmlElement private Integer age; private Date birthday; public Person(String name, Integer sex, Integer age) { this.name = name; this.sex = sex; this.age = age; } public Person() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")//json格式返回的日期格式化註解,日期格式json轉換預設是long類型的,如:144101141111111,格式化成yyyy-MM-dd HH:mm:ss格式 public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
Controller類
@Controller @RequestMapping("/mvc") public class HelloController { @RequestMapping("/helloXmlPerson/{name}") @ResponseBody public XmlActionResult helloXmlPerson(@PathVariable("name") String name){ XmlActionResult<Person> actionResult = new XmlActionResult<Person>(); actionResult.setCode("123"); actionResult.setMessage(name); Person person = new Person("xxxxx",1,32); actionResult.setData(person); return actionResult; } }
spring配置文件中新增
<mvc:annotation-driven/>
用於啟用@ResponseBody註解。
訪問對應得路徑後返回:
<xml> <code>123</code> <message>nihao</message> <name>xxxxx</name> <sex>1</sex> <age>32</age> </Person> </xml>