【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时候,一般我们都会要求手机验证码验证,邮箱验证,而这都依赖于第三方.这种情况下,我们一般会通 ...
随机推荐
- 开机流程与主引导分区(MBR)——鸟哥私房菜
在前篇随笔中,已经谈到了CMOS与BIOS,CMOS是记录各项硬件参数(包括系统时间.设备的I/O地址.CPU的电压和频率等)且嵌入到主板上面的存储器,BIOS是一个写入到主板上的韧体(韧体是写入到硬 ...
- Codeforces Round #303 (Div. 2) B 水 贪心
B. Equidistant String time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Matlab神经网络工具箱学习之二
螃蟹的分类 这个例子的目的是根据螃蟹的品种.背壳的长宽等等属性来判断螃蟹的性别,雄性还是雌性. 训练数据一共有六个属性: species, frontallip, rearwidth, length, ...
- Makefile---make内嵌函数及make命令显示 (九)
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 这一节我们讲一下make的函数,在之前的章节已经讲到了几个函数:wildcard.patsubs ...
- leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 适配IOS9中间遇到的一些问题
1 directory not found for option问题 警告如下: ld: warning: directory not found for option ‘-F/Application ...
- centOS 6.x 版本安装 node.js 4.x 以上版本的方法
由于 node 4.x 以上版本,通过编译源代码来安装,对 GCC 的版本有要求,而 centos 的 GCC 版本不够,更新 GCC 也很麻烦,所以只能通过别的方式解决. 这里主要介绍直接下载编译后 ...
- 【POJ3904】【P1202】水晶密码
说是莫比乌斯反演,其实只是玩儿玩儿内个miu函数而已…… 原题: wty 打算攻击 applepi 的用来存放机密数据的水晶系统. applepi 早有察觉,于是布置了一个密码系统来防备 wty ...
- 关于VC、MFC和ACCESS的一些使用问题
最近在用VC.MFC和ACCESS开发一些小工具. 由于操作系统和开发工具以及数据库版本都升级了,和当年有一些区别了(我这是有多老了--fuck--),遇到一些问题,贴在下面: 1,用什么连接AC ...
- urllib,urllib2,requests对比
#coding:utf-8 import urllib2 import urllib import httplib import socket import requests #实现以下几个方面内容: ...

