需求:
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>
<Person>
<name>xxxxx</name>
<sex>1</sex>
<age>32</age>
</Person>
</xml>
时间: 2024-11-03 05:31:52