Spring 3 MVC and XML example

Source: Internet
Author: User
Tags xml example

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:

  1. Spring 3.0.5.release
  2. JDK 1.6
  3. Eclipse 3.6
  4. 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

  1. Object annotated with jaxb
  2. Jaxb library existed in classpath
  3. "MVC: annotation-driven" is enabled
  4. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.