最近,想在自己的小项目中搭建一个Restful风格的服务接口api,项目用的spring mvc 3,听说spring mvc本身就能十分方便的支持restful的实现,于是查询了下资料,果然非常强大。

在一次偶然的#墙#外#(你懂的)状态下浏览到了一个老外的博客,举了几个入门例子十分经典,原文是E文+被墙状态,觉得有必要扒过来收藏学习下。

在本示例中,我们将向您展示如何将对象转换成xml格式并通过spring mvc框架返回给用户。

技术及环境:

  1. Spring 3.0.5.RELEASE
  2. JDK 1.6
  3. Eclipse 3.6
  4. Maven 3

1、添加项目依赖

不需要更多,你只要添加spring mvc的依赖即可:

  1. <properties>
  2. <spring.version>3.0.5.RELEASE</spring.version>
  3. </properties>
  4. <dependencies>
  5. <!-- Spring 3 dependencies -->
  6. <dependency>
  7. <groupId>org.springframework</groupId>
  8. <artifactId>spring-core</artifactId>
  9. <version>${spring.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-web</artifactId>
  14. <version>${spring.version}</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework</groupId>
  18. <artifactId>spring-webmvc</artifactId>
  19. <version>${spring.version}</version>
  20. </dependency>
  21. </dependencies>

2、实体类JavaBean

一个简单的JavaBean,添加了JAXB 注解,稍后将会被转换成xml。

JAXB已经包含在JDK1.6中,你不需要添加额外的依赖库,只需要使用注解,spring会自动将其转换为xml格式。

  1. import javax.xml.bind.annotation.XmlElement;
  2. import javax.xml.bind.annotation.XmlRootElement;
  3. @XmlRootElement(name = "coffee")
  4. public class Coffee {
  5. String name;
  6. int quanlity;
  7. public String getName() {
  8. return name;
  9. }
  10. @XmlElement
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. public int getQuanlity() {
  15. return quanlity;
  16. }
  17. @XmlElement
  18. public void setQuanlity(int quanlity) {
  19. this.quanlity = quanlity;
  20. }
  21. public Coffee(String name, int quanlity) {
  22. this.name = name;
  23. this.quanlity = quanlity;
  24. }
  25. public Coffee() {
  26. }
  27. }

3、Controller

添加@ResponseBody注解到你的方法返回值,在spring文档中没有太多的细节,它会自动处理转换。

  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.web.bind.annotation.PathVariable;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import com.mkyong.common.model.Coffee;
  7. @Controller
  8. @RequestMapping("/coffee")
  9. public class XMLController {
  10. @RequestMapping(value="{name}", method = RequestMethod.GET)
  11. public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {
  12. Coffee coffee = new Coffee(name, 100);
  13. return coffee;
  14. }
  15. }

4、mvc:annotation-driven

在你的spring配置文件中,启用mvc:annotation-driven注解。

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  12. <context:component-scan base-package="com.mkyong.common.controller" />
  13. <mvc:annotation-driven />
  14. </beans>

或者,你也可以添加spring-oxm.jar依赖,并用以下的MarshallingView处理转换,使用这种方法,你可以不用在方法中使用@ResponseBody注解。

  1. <beans ...>
  2. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
  3. <bean id="xmlViewer"
  4. class="org.springframework.web.servlet.view.xml.MarshallingView">
  5. <constructor-arg>
  6. <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  7. <property name="classesToBeBound">
  8. <list>
  9. <value>com.mkyong.common.model.Coffee</value>
  10. </list>
  11. </property>
  12. </bean>
  13. </constructor-arg>
  14. </bean>
  15. </beans>

5、示例结果

访问URL:http://localhost:8080/SpringMVC/rest/coffee/arabica

原文链接:http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

spring mvc实现Restful返回xml格式数据的更多相关文章

  1. Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据

    我们都知道Asp.net MVC自带的Action可以有多种类型,比如ActionResult,ContentResult,JsonResult……,但是很遗憾没有支持直接返回XML的XmlResul ...

  2. spring mvc 返回xml格式数据

    1.问题 : 因为业务需要,需要发送xml格式的数据,使用spring mvc 自己解析,就不用费心去自己搞这些东西. 2.解决: 新建一个实体类,直接在实体类中添加注解即可,如下: @XmlRoot ...

  3. 【Spring学习笔记-MVC-18.1】Spring MVC实现RESTful风格-同一资源,多种展现:xml-json-html

    概要 要实现Restful风格,主要有两个方面要讲解,如下: 1. 同一个资源,如果需要返回不同的形式,如:json.xml等: 不推荐的做法: /user/getUserJson /user/get ...

  4. 修改 mvc webapi 默认返回 json 格式

    web api 默认的已 xml 格式返回数据 现在开发一般都是以 json 格式为主 下面配置让 webapi 默认返回 json ,在需要返回 xml 时只需要加一个查询参数 datatype=x ...

  5. Spring MVC 学习笔记11 —— 后端返回json格式数据

    Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...

  6. Spring MVC 3.0 返回JSON数据的方法

    Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...

  7. Spring 全局异常拦截根据业务返回不同格式数据 自定义异常

    1.全局异常拦截:针对所有异常进行拦截 可根据请求自定义返回格式 2.自定义异常类 处理不同业务的异常 接下来开始入手代码: 1).自定义异常类 @ControllerAdvice//添加注解 记得开 ...

  8. Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)

    很多时候前端都需要调用后台服务实现交互功能,常见的数据交换格式多是JSON或XML,这里主要讲解Spring MVC为前端提供JSON格式的数据并实现与前台交互.RESTful则是一种软件架构风格.设 ...

  9. 应用Spring MVC发布restful服务是怎样的一种体验

            摘要:“约定优于配置”这是一个相当棒的经验,SOAP服务性能差.基于配置.紧耦合,restful服务性能好.基于约定.松耦合,现在我就把使用Spring MVC发布restful服务的 ...

随机推荐

  1. 【BIEE】01_下载安装BIEE(Business Intelligence)11g 11.1.1.9.0

    环境准备 安装文件 如果操作系统是64位,则下载64位版本,我安装的系统是64位的 1.下载所有安装文件 1.1 Oracle Database 11g R2 下载地址: http://www.ora ...

  2. UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)

    iOS5中,UIViewController新添加了几个方法: - (void)addChildViewController:(UIViewController *)childController N ...

  3. golang之路:mac下安装go

    1.下载dkg包 2.安装 3.vim .bash_profile export GOROOT=/usr/local/goexport GOPATH=$HOME/GoglandProjects/Pro ...

  4. Android的View 事件传递

    欢迎转载,请附出处: http://blog.csdn.net/as02446418/article/details/47422891 1.基础知识 (1) 全部 Touch 事件都被封装成了 Mot ...

  5. Maven的配置地址

    http://howtodoinjava.com/tools/eclipse/how-to-import-maven-remote-archetype-catalogs-in-eclipse/ htt ...

  6. IntelliJ IDEA 2014 付费版 免费版比较

    http://www.jetbrains.com/idea/features/editions_comparison_matrix.html Freemarker, Velocity IDE Feat ...

  7. (2) yum源配置-163

    1.获取yum源文件 登录http://mirrors.163.com/.help/centos.html,查看CentOS6的链接地址(右键点击“CentOS6”,选择复制链接地址),链接地址为:h ...

  8. 什么是Java Server Pages?

    JSP全称Java Server Pages,是一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开头以%>结束. JSP是一种Java servlet ...

  9. SPSS统计功能与模块对照表

    SPSS统计功能 - 应用速查表第一列为统计方法,中间为统计功能,最后一列为所在模块 1 ANOVA Models(单因素方差分析:简单因子) : 摘要 描述 方差 轮廓 - SPSS Base 2 ...

  10. Outlook2016删不掉主账户的解决方法

    控制面板>账户>邮件把配置文件删了 前两项和Outlook内部打开账户选项一样没用, 进第三个 重启Oulook的时候会提示重新建一个配置,就OK了!