Spring MVC学习(六)-------注解式控制器详解3
来源:互联网 发布:交通仿真软件 编辑:程序博客网 时间:2024/06/10 18:52
6.6.5.1、基本概念
写道
媒体类型格式:type/subtype(;parameter)?
type主类型,任意的字符串,如text,如果是*号代表所有;
subtype 子类型,任意的字符串,如html,如果是*号代表所有;
parameter 可选,一些参数,如Accept请求头的q参数, Content-Type的 charset参数。
详见http://tools.ietf.org/html/rfc2616#section-3.7
type主类型,任意的字符串,如text,如果是*号代表所有;
subtype 子类型,任意的字符串,如html,如果是*号代表所有;
parameter 可选,一些参数,如Accept请求头的q参数, Content-Type的 charset参数。
详见http://tools.ietf.org/html/rfc2616#section-3.7
Java代码
- @RequestMapping(value = "/ContentType", method = RequestMethod.GET)
- public String showForm() throws IOException {
- //form表单,使用application/x-www-form-urlencoded编码方式提交表单
- return "consumesproduces/Content-Type";
- }
- @RequestMapping(value = "/ContentType", method = RequestMethod.POST,
- headers = "Content-Type=application/x-www-form-urlencoded")
- public String request1(HttpServletRequest request) throws IOException {
- //①得到请求的内容区数据的类型
- String contentType = request.getContentType();
- System.out.println("========ContentType:" + contentType);
- //②得到请求的内容区数据的编码方式,如果请求中没有指定则为null
- //注意,我们的CharacterEncodingFilter这个过滤器设置了编码(UTF-8)
- //编码只能被指定一次,即如果客户端设置了编码,则过滤器不会再设置
- String characterEncoding = request.getCharacterEncoding();
- System.out.println("========CharacterEncoding:" + characterEncoding);
- //③表示请求的内容区数据为form表单提交的参数,此时我们可以通过request.getParameter得到数据(key=value)
- System.out.println(request.getParameter("realname"));
- System.out.println(request.getParameter("username"));
- return "success";
- }
Java代码
- @RequestMapping(value = "/request/ContentType", method = RequestMethod.POST,
- headers = "Content-Type=application/json")
- public String request2(HttpServletRequest request) throws IOException {
- //①表示请求的内容区数据为json数据
- InputStream is = request.getInputStream();
- byte bytes[] = new byte[request.getContentLength()];
- is.read(bytes);
- //②得到请求中的内容区数据(以CharacterEncoding解码)
- //此处得到数据后你可以通过如json-lib转换为其他对象
- String jsonStr = new String(bytes, request.getCharacterEncoding());
- System.out.println("json data:" + jsonStr);
- return "success";
- }
Java代码
- //请求的地址
- String url = "http://localhost:9080/springmvc-chapter6/request/ContentType";
- //①创建Http Request(内部使用HttpURLConnection)
- ClientHttpRequest request =
- new SimpleClientHttpRequestFactory().
- createRequest(new URI(url), HttpMethod.POST);
- //②设置请求头的内容类型头和内容编码(GBK)
- request.getHeaders().set("Content-Type", "application/json;charset=gbk");
- //③以GBK编码写出请求内容体
- String jsonData = "{\"username\":\"zhang\", \"password\":\"123\"}";
- request.getBody().write(jsonData.getBytes("gbk"));
- //④发送请求并得到响应
- ClientHttpResponse response = request.execute();
- System.out.println(response.getStatusCode());
Java代码
- @RequestMapping("/response/ContentType")
- public void response1(HttpServletResponse response) throws IOException {
- //①表示响应的内容区数据的媒体类型为html格式,且编码为utf-8(客户端应该以utf-8解码)
- response.setContentType("text/html;charset=utf-8");
- //②写出响应体内容
- response.getWriter().write("<font style='color:red'>hello</font>");
- }
Java代码
- @RequestMapping(value = "/response/ContentType", headers = "Accept=application/json")
- public void response2(HttpServletResponse response) throws IOException {
- //①表示响应的内容区数据的媒体类型为json格式,且编码为utf-8(客户端应该以utf-8解码)
- response.setContentType("application/json;charset=utf-8");
- //②写出响应体内容
- String jsonData = "{\"username\":\"zhang\", \"password\":\"123\"}";
- response.getWriter().write(jsonData);
- }
Java代码
- 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);
- }
Java代码
- @RequestMapping(value = "/response/ContentType", headers = "Accept=application/xml")
- public void response3(HttpServletResponse response) throws IOException {
- //①表示响应的内容区数据的媒体类型为xml格式,且编码为utf-8(客户端应该以utf-8解码)
- response.setContentType("application/xml;charset=utf-8");
- //②写出响应体内容
- String xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
- xmlData += "<user><username>zhang</username><password>123</password></user>";
- response.getWriter().write(xmlData);
- }
Java代码
- private static void xmlRequest() 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/xml");
- //③发送请求并得到响应
- 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 xmlData = new String(bytes, charset);
- System.out.println("charset : " + charset + ", xml data : " + xmlData);
- }
6.6.5.2、生产者消费者流程图
6.6.5.3、生产者、消费者限定
Java代码
- <!--Spring3.1开始的注解 HandlerMapping -->
- <bean
- class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
- <!--Spring3.1开始的注解 HandlerAdapter -->
- <bean
- class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
0 0
- Spring MVC学习(六)-------注解式控制器详解3
- Spring MVC学习(六)-------注解式控制器详解3
- Spring MVC学习(六)-------注解式控制器详解1
- Spring MVC学习(六)-------注解式控制器详解2
- Spring MVC学习(六)-------注解式控制器详解4
- Spring MVC学习(六)-------注解式控制器详解5
- Spring MVC学习(六)-------注解式控制器详解1
- Spring MVC学习(六)-------注解式控制器详解2
- Spring MVC学习(六)-------注解式控制器详解4
- Spring MVC学习(六)-------注解式控制器详解5
- Spring MVC详解(六)注解式控制器详解(1)
- Spring MVC详解(六)注解式控制器详解(1)
- 第六章、SpringMVC学习-注解式控制器详解(3)-Spring MVC 3 生产者、消费者请求限定
- 第六章 注解式控制器详解—— Spring MVC 3 生产者、消费者请求限定
- 码农小汪-Spring MVC注解式控制器简介
- Spring MVC学习(四)-------Controller接口控制器详解3
- Spring MVC学习(四)-------Controller接口控制器详解3
- Spring MVC 3 生产者、消费者请求限定 —— 第六章 注解式控制器详解——跟着开涛学SpringMVC
- maven tomcat插件 配置web.xml
- Linux下搭建svn版本库
- Spark SQL中的聚合(Aggregate)实现
- Windows网络编程之UDP通信
- Hadoop 中的Shuffle详解
- Spring MVC学习(六)-------注解式控制器详解3
- PHP 变量的数据类型 全解析
- Request对象及常用的操作
- Leetcode 300. Longest Increasing Subsequence
- mysql增删改查
- h5知识点小测试
- HTTPClient
- 轻松快速入门mysql的小基友redis
- 为什么while (*s++ = *t++);这句代码的作用是复制字符串?