spring mvc实现Restful返回xml格式数据
最近,想在自己的小项目中搭建一个Restful风格的服务接口api,项目用的spring mvc 3,听说spring mvc本身就能十分方便的支持restful的实现,于是查询了下资料,果然非常强大。
在一次偶然的#墙#外#(你懂的)状态下浏览到了一个老外的博客,举了几个入门例子十分经典,原文是E文+被墙状态,觉得有必要扒过来收藏学习下。
在本示例中,我们将向您展示如何将对象转换成xml格式并通过spring mvc框架返回给用户。
技术及环境:
- Spring 3.0.5.RELEASE
- JDK 1.6
- Eclipse 3.6
- Maven 3
1、添加项目依赖
不需要更多,你只要添加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、实体类JavaBean
一个简单的JavaBean,添加了JAXB 注解,稍后将会被转换成xml。
JAXB已经包含在JDK1.6中,你不需要添加额外的依赖库,只需要使用注解,spring会自动将其转换为xml格式。
- 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
添加@ResponseBody注解到你的方法返回值,在spring文档中没有太多的细节,它会自动处理转换。
- 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
在你的spring配置文件中,启用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、示例结果
访问URL:http://localhost:8080/SpringMVC/rest/coffee/arabica

原文链接:http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/
spring mvc实现Restful返回xml格式数据的更多相关文章
- Asp.net mvc返回Xml结果,扩展Controller实现XmlResult以返回XML格式数据
我们都知道Asp.net MVC自带的Action可以有多种类型,比如ActionResult,ContentResult,JsonResult……,但是很遗憾没有支持直接返回XML的XmlResul ...
- spring mvc 返回xml格式数据
1.问题 : 因为业务需要,需要发送xml格式的数据,使用spring mvc 自己解析,就不用费心去自己搞这些东西. 2.解决: 新建一个实体类,直接在实体类中添加注解即可,如下: @XmlRoot ...
- 【Spring学习笔记-MVC-18.1】Spring MVC实现RESTful风格-同一资源,多种展现:xml-json-html
概要 要实现Restful风格,主要有两个方面要讲解,如下: 1. 同一个资源,如果需要返回不同的形式,如:json.xml等: 不推荐的做法: /user/getUserJson /user/get ...
- 修改 mvc webapi 默认返回 json 格式
web api 默认的已 xml 格式返回数据 现在开发一般都是以 json 格式为主 下面配置让 webapi 默认返回 json ,在需要返回 xml 时只需要加一个查询参数 datatype=x ...
- Spring MVC 学习笔记11 —— 后端返回json格式数据
Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...
- Spring MVC 3.0 返回JSON数据的方法
Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...
- Spring 全局异常拦截根据业务返回不同格式数据 自定义异常
1.全局异常拦截:针对所有异常进行拦截 可根据请求自定义返回格式 2.自定义异常类 处理不同业务的异常 接下来开始入手代码: 1).自定义异常类 @ControllerAdvice//添加注解 记得开 ...
- Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)
很多时候前端都需要调用后台服务实现交互功能,常见的数据交换格式多是JSON或XML,这里主要讲解Spring MVC为前端提供JSON格式的数据并实现与前台交互.RESTful则是一种软件架构风格.设 ...
- 应用Spring MVC发布restful服务是怎样的一种体验
摘要:“约定优于配置”这是一个相当棒的经验,SOAP服务性能差.基于配置.紧耦合,restful服务性能好.基于约定.松耦合,现在我就把使用Spring MVC发布restful服务的 ...
随机推荐
- Mybatis 存在多个日志时设置日志
mybatis默认使用log4j,当有self4j这个日志jar包存在时会无法打印sql,请移除或者在工程启动时显示设置mybatis使用的日志类 log4j.logger.org.apache.ib ...
- mysql :安装过程中的问题
安装mysql 选择路径的问题 出现如下问题:说明之前安装过mysql,写在的不干净
- UICollectionView的header悬停
UICollectionView的header悬停,继承UICollectionViewFlowLayout,重写相关方法 // // StickyHeaderLayout.h // Wombat / ...
- C#CRC16 Modbus 效验算法
CRC校验(循环冗余校验)小知识 CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗 ...
- 安装java运行环境
1.查看java安装版本 执行命令java -version查看已安装java运行环境信息. 2.下载JDK 到sun官网下载需要的jdk版本,地址为:http://www.oracle.com/te ...
- Ansible 安装jdk
1. 在hosts文件添一个group,里面是你需要安装jdk的ip,如: [newhosts]192.168.2.155 ansible_ssh_user=hadoop ansible_ssh_pa ...
- java大批量数据导入(MySQL)
© 版权声明:本文为博主原创文章,转载请注明出处 最近同事碰到大批量数据导入问题,因此也关注了一下.大批量数据导入主要存在两点问题:内存溢出和导入速率慢. 内存溢出:将文件中的数据全部取出放在集合中, ...
- 不同手机根据坐标计算控件、图片的像素,px 与 dp, sp换算公式?
参考该帖子:http://www.cnblogs.com/bluestorm/p/3640786.html PPI = Pixels per inch,每英寸上的像素数,即 "像素密度&qu ...
- MyBatis_传入参数的问题
一.单个参数 1.基本数据类型 (1)直接使用 List<ChargeRuleDO> tests(long id); <select id="tests" res ...
- Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree
Problem A tree is a connected graph with no cycles. A rooted tree is a tree in which one special ver ...