SpringMVC Media Type

本文详细介绍了SpringMVC中关于媒体类型(Media Type)的基本概念,包括Content-Type和Accept的作用。讲解了如何在请求和响应中设置Content-Type以指定数据格式,以及客户端如何通过Accept请求头告诉服务器期望的响应类型。同时,讨论了Spring3.1开始支持的消费者和生产者限定功能,简化了处理不同媒体类型数据的方式。
摘要由CSDN通过智能技术生成

6.6.5、生产者、消费者限定

6.6.5.1、基本概念

首先让我们看一下通过HTTP协议传输的媒体类型及如何表示媒体类型:

 

一、Media Type:

互联网媒体类型,一般就是我们所说的MIME类型,用来确定请求的内容类型或响应的内容类型。

 写道
媒体类型格式:type/subtype(;parameter)?
type主类型,任意的字符串,如text,如果是*号代表所有;
subtype 子类型,任意的字符串,如html,如果是*号代表所有;
parameter 可选,一些参数,如Accept请求头的q参数, Content-Type的 charset参数。

详见http://tools.ietf.org/html/rfc2616#section-3.7

 常见媒体类型:

 

text/html : HTML格式          text/plain :纯文本格式             text/xml :XML格式

image/gif :gif图片格式          image/jpeg :jpg图片格式          image/png:png图片格式

 

application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)。

multipart/form-data : 当你需要在表单中进行文件上传时,就需要使用该格式;

 

application/xhtml+xml :XHTML格式               application/xml     : XML数据格式 

application/atom+xml  :Atom XML聚合格式    application/json    : JSON数据格式

application/pdf       :pdf格式                        application/msword  : Word文档格式

application/octet-stream : 二进制流数据(如常见的文件下载)。

 

在如tomcat服务器的 “conf/web.xml”中指定了扩展名到媒体类型的映射,在此我们可以看到服务器支持的媒体类型。

 

 

二、Content-Type:内容类型,即请求/响应的内容区数据的媒体类型;

2.1、请求头的内容类型,表示发送到服务器的内容数据的媒体类型;

request中设置请求头“Content-Type: application/x-www-form-urlencoded”表示请求的数据为key/value数据;

(1、控制器cn.javass.chapter6.web.controller.consumesproduces.contenttype.RequestContentTypeController

 

Java代码   收藏代码
  1.     @RequestMapping(value = "/ContentType", method = RequestMethod.GET)  
  2.     public String showForm() throws IOException {  
  3.         //form表单,使用application/x-www-form-urlencoded编码方式提交表单  
  4.         return "consumesproduces/Content-Type";  
  5.     }  
  6.       
  7.     @RequestMapping(value = "/ContentType", method = RequestMethod.POST,   
  8. headers = "Content-Type=application/x-www-form-urlencoded")  
  9.     public String request1(HttpServletRequest request) throws IOException {  
  10.         //①得到请求的内容区数据的类型  
  11.         String contentType = request.getContentType();   
  12.         System.out.println("========ContentType:" + contentType);  
  13.         //②得到请求的内容区数据的编码方式,如果请求中没有指定则为null  
  14.         //注意,我们的CharacterEncodingFilter这个过滤器设置了编码(UTF-8)  
  15.         //编码只能被指定一次,即如果客户端设置了编码,则过滤器不会再设置  
  16.         String characterEncoding = request.getCharacterEncoding();  
  17.         System.out.println("========CharacterEncoding:" + characterEncoding);  
  18.           
  19.         //③表示请求的内容区数据为form表单提交的参数,此时我们可以通过request.getParameter得到数据(key=value)  
  20.         System.out.println(request.getParameter("realname"));  
  21.         System.out.println(request.getParameter("username"));  
  22.         return "success";  
  23.     }  

 showForm功能处理方式:展示表单,且form的enctype="application/x-www-form-urlencoded",在提交时请求的内容类型头为“Content-Type:application/x-www-form-urlencoded”;

 

 

request1功能处理方法:只对请求头为“Content-Type:application/x-www-form-urlencoded”的请求进行处理(即消费请求内容区数据);

      request.getContentType()可以得到请求头的内容区数据类型(即Content-Type头的值)

      request.getCharacterEncoding()如“Content-Type:application/json;charset=GBK”,则得到的编码为“GBK”,否则如果你设置过滤器(CharacterEncodingFilter)则得到它设置的编码,否则返回null。

      request.getParameter()因为请求的内容区数据为application/x-www-form-urlencoded格式的数据,因此我们可以通过request.getParameter()得到相应参数数据。

 <

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值