Detailed description of Spring mvc implementation Restful returned xml format data instance, mvcrestful
Spring mvc implements Restful to return xml format data
Recently, I want to build a Restful service interface api in my own small project. spring mvc 3 is used in the project. I heard that spring mvc can easily support restful implementation, as a result, I queried the information, which is very powerful.
I browsed a foreigner's blog in the accidental # # (You know) state. I gave a few example of getting started. The original Article is in the E-file + walled state, I think it is necessary to collect and learn from my favorites.
In this example, we will show you how to convert objects into xml format and return them to users through spring mvc framework.
Technology and Environment:
Spring 3.0.5.RELEASE
JDK 1.6
Eclipse 3.6
Maven 3
1. Add project Dependencies
You only need to add spring mvc dependencies:
<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. entity-class JavaBean
A simple JavaBean with JAXB annotation added, which will be converted to xml later.
JAXB is already included in JDK1.6. You do not need to add additional dependent libraries. You only need to use annotations. spring will automatically convert them to xml format.
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 the @ ResponseBody annotation to your method return value. There is not much detail in the spring document, and it will automatically process the conversion.
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
Enable the mvc: annotation-driven annotation in your spring configuration file.
<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>
Alternatively, you can add a spring-oxm.jar dependency and process the conversion with the following MarshallingView. Using this method, you don't have to use the @ ResponseBody annotation in the 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. Sample Results
Access URL: http: // localhost: 8080/SpringMVC/rest/coffee/arabica
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!