国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - spring消息轉(zhuǎn)換器使用詳解

spring消息轉(zhuǎn)換器使用詳解

2020-11-28 12:22wdx330616 Java教程

這篇文章主要為大家詳細(xì)介紹了spring消息轉(zhuǎn)換器的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了spring消息轉(zhuǎn)換器的具體代碼,供大家參考,具體內(nèi)容如下

//domain

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.crazy.goods.tools;
/**
 * 0755-351512
 * @author Administrator
 *
 */
public class Phone {
  private String qno;
  private String number;
  public String getQno() {
    return qno;
  }
  public void setQno(String qno) {
    this.qno = qno;
  }
  public String getNumber() {
    return number;
  }
  public void setNumber(String number) {
    this.number = number;
  }
  
}

//消息轉(zhuǎn)換器  要實(shí)現(xiàn)一個(gè)抽象類AbstractHttpMessageConverter

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.crazy.goods.tools;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
 
public class MyMessageConvertor extends AbstractHttpMessageConverter<Phone> {
 
  /**
   * 將請(qǐng)求頭數(shù)據(jù)轉(zhuǎn)換成Phone
   */
  
  @Override
  protected Phone readInternal(Class<? extends Phone> arg0,
      HttpInputMessage msg) throws IOException,
      HttpMessageNotReadableException {
    //參數(shù)必須使用post提交必須在body中
    InputStream is=msg.getBody();
    BufferedReader br=new BufferedReader(new InputStreamReader(is));
    String param=br.readLine();
    String phone=param.split("=")[1];
    Phone phoneObj=new Phone();
    phoneObj.setQno(phone.split("-")[0]);
    phoneObj.setNumber(phone.split("-")[1]);
    return phoneObj;
  }
  /**
   * 當(dāng)前的轉(zhuǎn)換器支持轉(zhuǎn)換的類
   */
  @Override
  protected boolean supports(Class<?> arg0) {
    if(arg0==Phone.class){
      return true;
    }
    return false;
  }
  /**
   * 用于將返回的對(duì)象轉(zhuǎn)換成字符串顯示在網(wǎng)頁(yè)
   */
  @Override
  protected void writeInternal(Phone phone, HttpOutputMessage arg1)
      throws IOException, HttpMessageNotWritableException {
    String p=phone.getQno()+"-"+phone.getNumber();
    arg1.getBody().write(p.getBytes("UTF-8"));
  }
 
}

 

//springmvc.xml 要配置bean:消息轉(zhuǎn)換器,只有post提交方式才會(huì)被轉(zhuǎn)換器攔截

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    ">
  <!--springmvc只能掃描控制層 -->
  <context:component-scan base-package="com.crazy.goods">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
  </context:component-scan>
  
  <!--消息轉(zhuǎn)換器 必須使用post提交  -->
  <mvc:annotation-driven>
    <mvc:message-converters>
      <bean class="com.crazy.goods.tools.MyMessageConvertor">
        <property name="supportedMediaTypes">
          <list>
            <value>text/html;charset=UTF-8</value>
             <value>application/x-www-form-urlencoded</value>
          </list>
        </property>
      </bean>
    </mvc:message-converters>
  </mvc:annotation-driven>
</beans>

servlet測(cè)試

 
?
1
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.crazy.goods.servlet;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.crazy.goods.tools.Phone;
 
/**
* @author Administrator
* 創(chuàng)建時(shí)間:2017年7月1日下午3:11:27
*/
@Controller
public class ReservePageServelt {
 
// /**
// * forward:轉(zhuǎn)發(fā)
// * redirect:重定向
// * @param req
// * @param resp
// * @return
// * @throws ServletException
// * @throws IOException
// */
// @RequestMapping(value="/add",method={RequestMethod.GET})
// public String doGet(HttpServletRequest req, HttpServletResponse resp/*,@PathVariable("testid") String testid*/) throws ServletException, IOException {
// req.getRequestDispatcher("/reversegood.jsp").forward(req, resp);
// return "/reversegood.jsp";
// resp.getWriter().print(testid);
// }
 
 
//消息轉(zhuǎn)換器思路,
 
//原理就是將請(qǐng)求體或者請(qǐng)求頭的數(shù)據(jù)轉(zhuǎn)換為action方法的參數(shù),同時(shí)將方法的返回值的內(nèi)容轉(zhuǎn)換為響應(yīng)頭
//當(dāng)url路徑訪問(wèn)過(guò)來(lái)時(shí),看到使用了@RequestBody注解,這個(gè)注解標(biāo)識(shí)這個(gè)類要被消息轉(zhuǎn)換器處理,就會(huì)springmvcxml文件中讀到消息轉(zhuǎn)換器,然后進(jìn)入supports方法
//判斷這個(gè)類是否被指定的轉(zhuǎn)換器支持,如果支持,就調(diào)用readInternal方法,進(jìn)行切割,然后將值傳遞到對(duì)象中,處理完成為對(duì)象之后,就會(huì)調(diào)用writeInternal轉(zhuǎn)換為響應(yīng)頭
@RequestMapping(value="/add")
@ResponseBody
public Phone messageConvertor( @RequestBody Phone phone,HttpServletResponse response) {
System.out.println(phone.getQno()+phone.getNumber());
return phone;
 
}
 
}

總結(jié):消息轉(zhuǎn)換器的原理就是,自定義將請(qǐng)求體的數(shù)據(jù)轉(zhuǎn)換為形參(對(duì)象),然后將方法的返回值內(nèi)容轉(zhuǎn)換為響應(yīng)頭

步驟:

當(dāng)url路徑訪問(wèn)過(guò)來(lái)時(shí),看到使用了@RequestBody注解,這個(gè)注解標(biāo)識(shí)這個(gè)類要被消息轉(zhuǎn)換器處理,就會(huì)springmvcxml文件中讀到消息轉(zhuǎn)換器,然后進(jìn)入supports方法
判斷這個(gè)類是否被指定的轉(zhuǎn)換器支持,如果支持,就調(diào)用readInternal方法,進(jìn)行切割,然后將值傳遞到對(duì)象中.

處理完成為對(duì)象之后,就會(huì)調(diào)用writeInternal轉(zhuǎn)換為響應(yīng)頭

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄版视频在线观看 | 在线国产视频 | 中文字幕国产视频 | 日韩在线成人 | 天天操夜夜操av | 中文字幕在线观看一区二区三区 | 精品视频一区在线观看 | 亚洲一区二区三区四区的 | 欧美hdfree性xxxx| 日韩av片无码一区二区不卡电影 | 国产精品女同一区二区免费站 | 国产精品一卡二卡三卡 | 日日操狠狠操 | 日韩欧美精品在线 | 中文字幕在线观看不卡视频 | 黄色一级网站视频 | 欧美激情视频一区二区三区在线播放 | 久久h| 日韩欧美在线一区二区 | 婷婷亚洲五月 | 亚洲永久免费 | 国产精品成人一区二区 | 日韩在线免费 | 99久久久| 青青久久av北条麻妃黑人 | 国产成人精品免高潮在线观看 | 国产一区二区三区免费在线观看 | 尤物网址 | 中文字幕亚洲专区 | 91精品国产色综合久久 | 精品成人18| www.久久精品 | 91国产视频在线 | 黄色毛片免费网站 | 午夜视频在线播放 | 最新毛片在线观看 | 亚洲精品三级 | 日韩电影免费在线观看 | 欧美日韩精品一区二区三区 | 亚洲一一在线 | 久久人 |