媒体类型:

text/html, text/plain, text/xml
image/gif, image/jpg, image/png
application/x-www-form-urlencodeed, application/json, application/xml 有q参数,q参数为媒体类型的质量因子,越大则优先权越高(从0到1)
multipart/form-data

内容类型:Content-Type:MIME  有charset参数

服务器端有header, 客户端有Accept,扩展名,参数-ContentNegotiatingViewResolver(简单方法)

spring3.1通过consume和product来限定

窄化时是覆盖而非继承,其他(header,param)是继承

服务器端header详解

1、form get

@RequestMapping("/content")
public ModelAndView showForm(@ModelAttribute("user") User user){
ModelAndView mv = new ModelAndView();
mv.setViewName("hello");
return mv;
}

2、form post x

@RequestMapping(value="/content", method=RequestMethod.POST, headers="Content-Type=application/x-www-form-urlencoded")
public ModelAndView request1(HttpServletRequest request){
//System.out.println("/content post"); String contentType=request.getContentType();
System.out.println("contentType:"+contentType); String characterEncoding=request.getCharacterEncoding();
System.out.println("characterEncoding:"+characterEncoding); System.out.println(request.getParameter("c")); ModelAndView mv = new ModelAndView();
mv.setViewName("success");
return mv;
}

3、form post json

@RequestMapping(value="/content", method=RequestMethod.POST, headers="Content-Type=application/json")
public void request2(HttpServletRequest request, HttpServletResponse response) throws IOException{
System.out.println("/json content post");
System.out.println("charset:"+request.getCharacterEncoding()); InputStream is = request.getInputStream();
byte bytes[] = new byte[request.getContentLength()];
is.read(bytes); String jsonStr = new String(bytes, request.getCharacterEncoding());
System.out.println("json data:"+jsonStr); ModelAndView mv = new ModelAndView();
mv.setViewName("success");
return mv; }

4、form post x ClientHttpRequest

@RequestMapping(value="/content", method=RequestMethod.POST, headers="Content-Type=application/x-www-form-urlencoded")
public void request(HttpServletResponse response) throws IOException, URISyntaxException{
System.out.println("/x content post"); String url = "http://localhost:8080/springmvc/content";
ClientHttpRequest request = new SimpleClientHttpRequestFactory().createRequest(new URI(url), HttpMethod.POST);
request.getHeaders().set("Content-Type", "application/json;charset=gbk");
String jsonData = "{\"username\":\"zhang\"}";
request.getBody().write(jsonData.getBytes("gbk"));
request.execute();
System.out.println("status:"+response.getStatus()); response.setContentType("text/html;charset=utf-8");
response.getWriter().write("json response"); }

1-2正常情况

1-3没法设置json,所以1-4-3-4

2注释掉,因为get想直接到post x

ClientHttpRequest与真正的HttpServletResponse不同,有些方法不太一样。

客户端Accept

不太了解,client的方法

private static void jsonRequest() throws IOException, URISyntaxException {
//请求的地址
String url = "http://localhost:9080/springmvc-chapter6/response/ContentType";
//①创建Http Request(内部使用HttpURLConnection)
ClientHttpRequest request =
new SimpleClientHttpRequestFactory().
createRequest(new URI(url), HttpMethod.POST);
//②设置客户端可接受的媒体类型(即需要什么类型的响应体数据)
request.getHeaders().set("Accept", "application/json");
//③发送请求并得到响应
ClientHttpResponse response = request.execute();
//④得到响应体的编码方式
Charset charset = response.getHeaders().getContentType().getCharSet();
//⑤得到响应体的内容
InputStream is = response.getBody();
byte bytes[] = new byte[(int)response.getHeaders().getContentLength()];
is.read(bytes);
String jsonData = new String(bytes, charset);
System.out.println("charset : " + charset + ", json data : " + jsonData);
}

spring3.1

需要添加的bean

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
consumes = {application/json};
produces = {application/json}

总结:有些方法不太熟练

HttpServletRequest req
req.getContentType();
req.getCharacterEncoding();
req.getParameter("c"); //form name c
req.getContentLength(); HttpServletResponse res
res.setContentType("text/html;charset=utf-8");
res.getWriter().write("json response"); ClientHttpRequest req
req.getHeaders().set("Content-Type", "application/json;charset=gbk");
req.getBody().write(jsonData.getBytes("gbk"));
req.execute();

  

springmvc 开涛 生产者/消费者的更多相关文章

  1. springmvc 开涛 注解式控制器

    版本 定义处理器类 处理器映射适配器 备注 支持的注解 2.5前 controller       2.5 注解 DefaultAnnotationHandlerMapping AnnotationM ...

  2. springMVC 开涛 数据绑定

    纸上得来终觉浅,绝知此事要躬行. 一.@requestParam //使用方法URL:?username="sfp" test(@RequestParam(value=" ...

  3. springmvc 开涛 拦截器

    拦截器有三个方法:preHandle, postHandle, afterCompletion ***-servlet.xml <bean name="/test" clas ...

  4. springmvc 开涛 数据验证

    两种方式:编程和声明. 编程需要:验证器,控制器,servlet.xml,错误码设置 声明需要:加jar包,控制器,跟孔浩讲得类似 错误消息设置的两种方式:硬编码:从资源文件中读取(默认,自定义).

  5. springMVC 开涛 Controller接口控制器

    通过注解实现控制器类,所以不用看Controller接口了.把之前的笔记保存下. 笔记(图片):http://pan.baidu.com/s/1mgMNDna 第三章看不太懂,3.2 3.3.只了解到 ...

  6. 【转】跟着开涛学SpringMVC

    跟着开涛学SpringMVC 第一章源代码下载 博客分类: 跟开涛学SpringMVC 跟开涛学SpringMVC  源代码请到附件中下载. 其他下载: 跟着开涛学SpringMVC 第一章源代码下载 ...

  7. Java多线程14:生产者/消费者模型

    什么是生产者/消费者模型 一种重要的模型,基于等待/通知机制.生产者/消费者模型描述的是有一块缓冲区作为仓库,生产者可将产品放入仓库,消费者可以从仓库中取出产品,生产者/消费者模型关注的是以下几个点: ...

  8. java 生产者消费者问题 并发问题的解决

    引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...

  9. MVC异常日志生产者消费者模式记录(异常过滤器)

    生产者消费者模式 定义自己的异常过滤器并注册 namespace Eco.Web.App.Models { public class MyExceptionAttribute : HandleErro ...

随机推荐

  1. adb设置逍遥游

    . adb设置模拟器属性imei.imsi.手机号.sim卡号2. adb设置充电模式3. 开启|关闭飞行模式4. 获取所有已安装程序apk路径和包名5. adb对指定设备执行指令6. 安装应用7. ...

  2. CentOS 6.3安装配置supervisor进程管理工具

    1. Supervisor是一个C/S系统,它可以在类unix操作系统让用户来监视和控制后台服务进程的数量,一个很重要的功能就是监控服务器的主要后台进程,并在出现问题是自动重启. 2. 根据服务器上的 ...

  3. Redis启动与使用

    在redis文件夹下,启动redis服务端的命令如下: .\redis-server 也可以指定要加载的配置文件,如下: .\redis-server ..\redis.conf 启动redis客户端 ...

  4. tag-SMASS-1

    SMASS 是在vasp的DFTMD中决定着系综的类型,在手册中给出的该参数具体信息如下: SMASS = -3 | -2 | -1 | [real] ≥ 0 Default: SMASS = -3 ...

  5. 第五章 二叉树(a)树

  6. Lunch Time(费用流变型题,以时间为费用)

    Lunch Time http://acm.hdu.edu.cn/showproblem.php?pid=4807 Time Limit: 4000/2000 MS (Java/Others)     ...

  7. Bom对象介绍

    1.windows对象 1.windows对象 1_1.alert:显示一段消息和确认按钮的弹出的警告框 我们平时用的alert的全称就是 window.alert("hahah" ...

  8. 零基础学习hadoop到上手工作线路指导(初级篇)

    零基础学习hadoop,没有想象的那么困难,也没有想象的那么容易.在刚接触云计算,曾经想过培训,但是培训机构的选择就让我很纠结.所以索性就自己学习了.整个过程整理一下,给大家参考,欢迎讨论,共同学习. ...

  9. metasploit渗透测试指南概要整理

    一.名词解释 exploit 测试者利用它来攻击一个系统,程序,或服务,以获得开发者意料之外的结果.常见的 有内存溢出,网站程序漏洞利用,配置错误exploit. payload 我们想让被攻击系统执 ...

  10. binlog怎样参与mysql recover的

    转自  Louis Hust's Blog MySQL两阶段提交 29 July 2015 参数介绍 两阶段提交 什么情况下会出现binlog写入了,但是实际这条数据不存在库中? 参数介绍 innod ...