In spring 3, one of the feature of "MVC: annotation-driven", is support for convert object to/from XML file, if jaxb is in project classpath.
In this tutorial, we show you how to convert a return object into XML format and return it back to user via spring @ MVC framework.
Technologies used:
- Spring 3.0.5.release
- JDK 1.6
- Eclipse 3.6
- Maven 3
Jaxb in jdk6
Jaxb is wrongly ded in jdk6, so, you do not need to include jaxb library manually, As long as object is annotated with jaxb annotation, spring will convert it into XML format automatically.2. model + jaxb
A simple pojo model and annotatedJaxb Annotation, Later convert this object into XML output.
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. Controller
Add"@ Responsebody"In the method return value, no much detail in the spring documentation.
As I know, when spring see
- Object annotated with jaxb
- Jaxb library existed in classpath
- "MVC: annotation-driven" is enabled
- Return method annotated with @ responsebody
It will handle the conversion automatically.
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: annotation-driven
In one of your spring configuration XML file, enable"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>
Note
Alternatively, you can declares"Spring-oxm.jar"Dependency and include followingMarshallingView
, To handle the conversion. With this method, you don't need annotate@ ResponsebodyIn your method.
<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. Demo
URL:Http: // localhost: 8080/springmvc/rest/coffee/arabica
Download source codedownload it-SpringMVC-XML-Example.zip (7 KB)