在Spring 3中,“ mvc:annotation-driven ”的功能之一是支持将对象转换为XML文件或从XML文件转换对象(如果JAXB在项目类路径中)。
在本教程中,我们向您展示如何将返回对象转换为XML格式,并通过Spring @MVC框架将其返回给用户。
使用的技术:
- Spring 3.0.5。发布
- JDK 1.6
- Eclipse 3.6
- Maven 3
JDK6中的JAXB
JDK6中包含JAXB,因此,您不需要手动包含JAXB库,只要使用JAXB注释对对象进行注释,Spring就会自动将其转换为XML格式。
1.项目依赖
没有额外的依赖关系,您只需要在Maven pom.xml
包含Spring MVC。
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
2.型号+ JAXB
一个简单的POJO模型,并用JAXB批注进行批注 ,随后将该对象转换为XML输出。
package com.mkyong.common.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "coffee")
public class Coffee {
String name;
int quanlity;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getQuanlity() {
return quanlity;
}
@XmlElement
public void setQuanlity(int quanlity) {
this.quanlity = quanlity;
}
public Coffee(String name, int quanlity) {
this.name = name;
this.quanlity = quanlity;
}
public Coffee() {
}
}
3.控制器
在方法返回值中添加“ @ResponseBody ”,在Spring文档中没有太多细节。
据我所知,当春天看到
- 用JAXB注释的对象
- JAXB库存在于类路径中
- 启用了“ mvc:annotation-driven”
- 用@ResponseBody注释的返回方法
它将自动处理转换。
package com.mkyong.common.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mkyong.common.model.Coffee;
@Controller
@RequestMapping("/coffee")
public class XMLController {
@RequestMapping(value="{name}", method = RequestMethod.GET)
public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {
Coffee coffee = new Coffee(name, 100);
return coffee;
}
}
4. mvc:注释驱动
在您的Spring配置XML文件之一中,启用“ mvc:annotation-driven
”。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.mkyong.common.controller" />
<mvc:annotation-driven />
</beans>
注意
或者,您可以声明“ spring-oxm.jar ”依赖项,并包括以下MarshallingView
来处理转换。 使用此方法,您无需在方法中注释@ResponseBody 。<beans ...> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean id="xmlViewer" class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg> <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="classesToBeBound"> <list> <value>com.mkyong.common.model.Coffee</value> </list> </property> </bean> </constructor-arg> </bean> </beans>
5.演示
网址: http:// localhost:8080 / SpringMVC / rest / coffee / arabica
下载源代码
下载它– SpringMVC-XML-Example.zip (7 KB)
参考文献
翻译自: https://mkyong.com/spring-mvc/spring-3-mvc-and-xml-example/