【Spring学习笔记-MVC-18.1】Spring MVC实现RESTful风格-同一资源,多种展现:xml-json-html
概要
1. 同一个资源,如果需要返回不同的形式,如:json、xml等;
- /user/getUserJson
- /user/getUserXML
2. 对同一资源的CRUD操作
- /user/addUser/
- /user/getUser/123
- /user/deleteUser/123
- /user/updateUser/123
内容协商介绍
RESTful服务中很重要的一个特性即是同一资源,多种表述,也即如下面描述的三种方式:
- 方式1:使用http request header: Accept
- GET /user/123 HTTP/1.1
- Accept: application/xml //将返回xml格式数据
- GET /user/123 HTTP/1.1
- Accept: application/json //将返回json格式数据
- 方式2:使用扩展名
- /user/123.xml 将返回xml格式数据
- /user/123.json 将返回json格式数据
- /user/123.html 将返回html格式数据
- 方式3:使用参数
- /user/123?format=xml //将返回xml数据
- /user/123?format=json //将返回json数据
以上三种优缺点比较
- 使用Accept header ==>由于浏览器的差异,一般不使用此种方式
- chrome:
- Accept:application/xml,application/xhtml+xml,textml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
- firefox:
- Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
- IE8:
- Accept:image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
- 使用扩展名
- 使用参数
使用ContentNegotiatingViewResolver内容协商视图解析器



- /user/123.json 或
- /user/123?format=json
- /user/123.xml
- /user/123?format=xml
- xpp3_min-xxx.jar;
- xstream-xxx.jar。
- /user/123.html
- /user/123?format=html
- /user/123
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- 扫描web包,应用Spring的注解 --><context:component-scan base-package="com.ll.web"/><mvc:annotation-driven/><!-- 协商多种视图解析器 --><bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"p:order="0"p:ignoreAcceptHeader="true"p:favorPathExtension="true"p:favorParameter="true"p:parameterName="format"p:defaultContentType="text/html"><!-- 用来定义哪些扩展名(如:/user/123.json),或协商参数值(如:/user/123?format=xml)是可识别的 --><property name="mediaTypes"><map><entry key="html" value="text/html" /><entry key="xml" value="application/xml" /><entry key="json" value="application/json" /></map></property><property name="defaultViews"><list><!-- for application/json --><bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/><!-- for application/xml --><bean class="org.springframework.web.servlet.view.xml.MarshallingView"><property name="marshaller"><bean class="org.springframework.oxm.xstream.XStreamMarshaller"/></property></bean></list></property></bean><!-- Excel及PDF视图解析器配置 --><bean class="org.springframework.web.servlet.view.BeanNameViewResolver"p:order="10" /><!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面,默认优先级最低 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"p:order="100"p:viewClass="org.springframework.web.servlet.view.JstlView"p:prefix="/resource/jsp/"p:suffix=".jsp" /><!-- spring mvc对静态资源的访问 --><mvc:resources mapping="/resource/**" location="/resource/**" /></beans>
控制层代码

测试
1. json格式

2. xml格式




4. 不带任何参数



其他
博客:
附件列表
【Spring学习笔记-MVC-18.1】Spring MVC实现RESTful风格-同一资源,多种展现:xml-json-html的更多相关文章
- 【Spring学习笔记-MVC-15.1】Spring MVC之异常处理=404界面
作者:ssslinppp 异常处理请参考前篇博客:<[Spring学习笔记-MVC-15]Spring MVC之异常处理>http://www.cnblogs.com/sssl ...
- 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传
作者:ssslinppp 1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...
- 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回
作者:ssslinppp 时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...
- Spring学习笔记(一)—— Spring介绍及入门案例
一.Spring概述 1.1 Spring是什么 Spring是一个开源框架,是于2003年兴起的一个轻量级的Java开发框架, 由Rod Johnson 在其著作<Expert one on ...
- Spring学习笔记4—流程(Spring Web Flow)
Spring Web Flow是Spring框架的子项目,作用是让程序按规定流程运行. 1 安装配置Spring Web Flow 虽然Spring Web Flow是Spring框架的子项目,但它并 ...
- Spring学习笔记(二)Spring基础AOP、IOC
Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...
- Spring学习笔记(四)-- Spring事务全面分析
通过本系列的文章对Spring的介绍,我们对Spring的使用和两个核心功能IOC.AOP已经有了初步的了解,结合我个人工作的情况,因为项目是金融系 统.那对事务的控制是不可缺少的.而且是很严格的控制 ...
- Spring学习笔记(四)—— Spring中的AOP
一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- spring学习笔记(二)spring中的事件及多线程
我们知道,在实际开发中为了解耦,或者提高用户体验,都会采用到异步的方式.这里举个简单的例子,在用户注册的sh时候,一般我们都会要求手机验证码验证,邮箱验证,而这都依赖于第三方.这种情况下,我们一般会通 ...
随机推荐
- python报错
报错1 UnboundLocalError: local variable 'x' referenced before assignment 定义了一个全局参数,但是在函数中直接改变参数值,就会报这个 ...
- Java 返回一个整数的各个数字之和的一种方法
public static long sumDigits(long n){ long total=0; long number=n; while(number!=0){ total=total+num ...
- 主机控制器接口(HCI)
主机控制器接口(HCI)提供了一种访问蓝牙硬件能力的通用接口. HCI固件通过访问基带命令.链路管理器命令.硬件状态寄存器.控制寄存器以及事件寄存器实现对蓝牙硬件的HCI命令. HCI是通过包的方式来 ...
- canvas-绘制时钟
把最近学到的一些canvas技能全部发上来,刚开始写博客,感觉还不太习惯,不过我相信慢慢就会习惯了.不啰嗦了,把代码送上,看不懂的话可以先去学习下基础教程,把基础学好了也就能看懂了. <!DOC ...
- kuangbin_MST A (POJ 1251)
模板题 Kruskal直接过 调试时候居然在sort(edge + 1, edge + 1 + m)上浪费好多时间... 不过本着ACMer的心态自然要测试一下两种方法分别的速度 Kruskal : ...
- Valgrind使用[转]
简介 调试程序有很多方法,例如向屏幕上打印消息,使用调试器,或者只需仔细考虑程序如何运行,并对问题进行有根有据的猜测. 在修复 bug 之前,首先要确定在源程序中的位置.例如,当一个程序产生崩溃或生成 ...
- Twitter 登录和分享
继上面一片介绍了FaceBook的登录和分享,现在再来实现Twitter的登录和分享. 1.首先要说明的是,我没找到官方提供的SDK,查阅很多文章都提到了一个帮助实现的包Twitter4j.jar ...
- 利用KMeans聚类进行航空公司客户价值分析
准确的客户分类的结果是企业优化营销资源的重要依据,本文利用了航空公司的部分数据,利用Kmeans聚类方法,对航空公司的客户进行了分类,来识别出不同的客户群体,从来发现有用的客户,从而对不同价值的客户类 ...
- 常规SQL注入脚本
一:union报错注入 猜字段长度:order by 28 先显示位http://127.0.0.1/sql.php?cmd=-1 UNION SELECT 1,2,3,4,5,6,7,8,9 当前数 ...
- C#Linq中的Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods,skip,take,takewhile,skipwhile,编译查询等
我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Intersect操作和Top/Bottom操作和Paging操作和SqlMethods操作 . Union Al ...

