欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot @PostMapping接收HTTP請求的流數(shù)據(jù)問題

 更新時間:2023年02月27日 09:01:42   作者:lookbbs  
這篇文章主要介紹了SpringBoot @PostMapping接收HTTP請求的流數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

@PostMapping接收HTTP請求的流數(shù)據(jù)

@PostMapping("/test")
public String pushMessage(@RequestBody byte[] data) throws Exception {
    String json = URLDecoder.decode(new String(data, DEFAULT_CHARSET), DEFAULT_CHARSET);
    log.info(">>> 接收CP推送的消息:{}", json);
 
    JSONObject jsonObject = JacksonUtils.jsonToBean(json, JSONObject.class);
    System.out.println(jsonObject.get("key"));
    return “success”
}

Client 請求

try {
    //創(chuàng)建連接
    URL url = new URL(ADD_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestMethod("POST");
    connection.setUseCaches(false);
    connection.setInstanceFollowRedirects(true);
    //application/x-javascript 
    //text/xml->xml數(shù)據(jù) 
    //application/x-javascript->json對象 
    //application/x-www-form-urlencoded->表單數(shù)據(jù) 
    //application/json;charset=utf-8 -> json數(shù)據(jù)
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("accept", "*/*");
    connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 
    connection.connect();
 
    //POST請求
    DataOutputStream out = new DataOutputStream(connection.getOutputStream());
 
    JSONObject data = new JSONObject();
    data.element("key", "這是一條測試數(shù)據(jù)");
    
    out.writeBytes(data.toString());
    out.flush();
    out.close();
 
    //讀取響應(yīng)
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String lines;
    StringBuffer sb = new StringBuffer("");
    while ((lines = reader.readLine()) != null) {
        lines = new String(lines.getBytes(), "utf-8");
        sb.append(lines);
    }
    System.out.println(sb);
    reader.close();
    // 斷開連接
    connection.disconnect();
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

關(guān)于@PostMapping注解解析

開發(fā)過程IDEA提示如將@RequestMapping(value="/abc" , method = “RequestMethod.POST”)替換成@PostMapping?,F(xiàn)對@PostMapping的實(shí)現(xiàn)。

@PostMapping是一個復(fù)合注解,Spring framework 4.3引入了@RequestMapping注釋的變體,以更好地表示帶注釋的方法的語義,作為@RequestMapping(method = RequestMethod.POST)的快捷方式。

也就是可以簡化成@PostMapping(value="/abc" )即可,主要是方便識記。

下面很多方法都是對應(yīng)著@RequestMapping的標(biāo)記的別名。

@RequestMapping(value = “”, path = “”, params = “”, headers = “”,consumes = “”, produces = “”)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.POST)
public @interface PostMapping {

	/**
	 * RequestMapping 的別名,
	 */
	@AliasFor(annotation = RequestMapping.class)
	String name() default "";

	/**
	 *RequestMapping#value的別名, 默認(rèn)為空字符串,一般需要自己填寫
	 */
	@AliasFor(annotation = RequestMapping.class)
	String[] value() default {};

	/**
	 * RequestMapping#path的別名
	 */
	@AliasFor(annotation = RequestMapping.class)
	String[] path() default {};

	/**
	 * RequestMapping#params的別名
	 */
	@AliasFor(annotation = RequestMapping.class)
	String[] params() default {};

	/**
	 * RequestMapping#headers的別名
	 */
	@AliasFor(annotation = RequestMapping.class)
	String[] headers() default {};

	/**
	 * RequestMapping#consumes的別名
	 */
	@AliasFor(annotation = RequestMapping.class)
	String[] consumes() default {};

	/**
	 * RequestMapping#produces的別名
	 */
	@AliasFor(annotation = RequestMapping.class)
	String[] produces() default {};

}

其他變體如下:

@GetMapping、@PutMapping、@PatchMapping和@DeleteMapping,與@PostMapping實(shí)現(xiàn)類似

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatisplus 的SQL攔截器實(shí)現(xiàn)關(guān)聯(lián)查詢功能

    mybatisplus 的SQL攔截器實(shí)現(xiàn)關(guān)聯(lián)查詢功能

    大家都知道m(xù)ybatisplus不支持關(guān)聯(lián)查詢,后來學(xué)習(xí)研究發(fā)現(xiàn)mybatisplus的SQL攔截器可以實(shí)現(xiàn)這一操作,下面小編給大家分享我的demo實(shí)現(xiàn)基本的關(guān)聯(lián)查詢功能沒有問題,對mybatisplus關(guān)聯(lián)查詢相關(guān)知識感興趣的朋友一起看看吧
    2021-06-06
  • java中構(gòu)造方法及this關(guān)鍵字的用法實(shí)例詳解(超詳細(xì))

    java中構(gòu)造方法及this關(guān)鍵字的用法實(shí)例詳解(超詳細(xì))

    大家都知道,java作為一門內(nèi)容豐富的編程語言,其中涉及的范圍是十分廣闊的,下面這篇文章主要給大家介紹了關(guān)于java中構(gòu)造方法及this關(guān)鍵字用法的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • kafka-console-consumer.sh使用2次grep管道無法提取消息的解決

    kafka-console-consumer.sh使用2次grep管道無法提取消息的解決

    這篇文章主要介紹了kafka-console-consumer.sh使用2次grep管道無法提取消息的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 運(yùn)用springboot搭建并部署web項(xiàng)目的示例

    運(yùn)用springboot搭建并部署web項(xiàng)目的示例

    這篇文章主要介紹了運(yùn)用springboot搭建并部署web項(xiàng)目的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • 淺談Java引用和Threadlocal的那些事

    淺談Java引用和Threadlocal的那些事

    這篇文章主要介紹了Java引用和Threadlocal的那些事,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • SpringBoot詳細(xì)介紹SPI機(jī)制示例

    SpringBoot詳細(xì)介紹SPI機(jī)制示例

    這篇文章主要介紹了深入解析Spring Boot的SPI機(jī)制詳情,SPI是JDK內(nèi)置的一種服務(wù)提供發(fā)現(xiàn)機(jī)制,可以用來啟用框架擴(kuò)展和替換組件,主要用于框架中開發(fā),更多相關(guān)介紹,感興趣的小伙伴可以參考一下下面文章內(nèi)容
    2022-08-08
  • 通過反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解

    通過反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解

    這篇文章主要介紹了通過反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • 了解spring中的CloudNetflix Hystrix彈性客戶端

    了解spring中的CloudNetflix Hystrix彈性客戶端

    這篇文章主要介紹了了解spring中的CloudNetflix Hystrix彈性客戶端,客戶端彈性模式是在遠(yuǎn)程服務(wù)發(fā)生錯誤或表現(xiàn)不佳時保護(hù)遠(yuǎn)程資源(另一個微服務(wù)調(diào)用或者數(shù)據(jù)庫查詢)免于崩潰。,需要的朋友可以參考下
    2019-06-06
  • SpringBoot屬性注入的多種方式實(shí)例

    SpringBoot屬性注入的多種方式實(shí)例

    在 SpringBoot中,提供了一種新的屬性注入方式,支持各種java基本數(shù)據(jù)類型及復(fù)雜類型的注入,下面這篇文章主要給大家介紹了關(guān)于SpringBoot屬性注入的多種方式,需要的朋友可以參考下
    2021-10-10
  • Java多線程正確使用倒計(jì)時協(xié)調(diào)器CountDownLatch方法詳解

    Java多線程正確使用倒計(jì)時協(xié)調(diào)器CountDownLatch方法詳解

    這篇文章主要為大家介紹了Java多線程倒計(jì)時協(xié)調(diào)器CountDownLatch的正確使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09

最新評論