SpringBoot @PostMapping接收HTTP請求的流數(shù)據(jù)問題
@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),希望能給大家一個參考,也希望大家多多支持腳本之家。
- Spring Boot 中的 @PutMapping 注解原理及使用小結(jié)
- Spring中@RequestMapping、@PostMapping、@GetMapping的實(shí)現(xiàn)
- Spring MVC @GetMapping和@PostMapping注解的使用方式
- 詳解SpringBoot中@PostMapping注解的用法
- Java @PostMapping和@GetMapping方法使用詳解
- 聊聊@RequestMapping和@GetMapping @PostMapping的區(qū)別
- 如何解決@PutMapping或@PostMapping接收String類型參數(shù)多兩個“引號問題
相關(guā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-06java中構(gòu)造方法及this關(guān)鍵字的用法實(shí)例詳解(超詳細(xì))
大家都知道,java作為一門內(nèi)容豐富的編程語言,其中涉及的范圍是十分廣闊的,下面這篇文章主要給大家介紹了關(guān)于java中構(gòu)造方法及this關(guān)鍵字用法的相關(guān)資料,需要的朋友可以參考下2022-04-04kafka-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)目的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06通過反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解
這篇文章主要介紹了通過反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12了解spring中的CloudNetflix Hystrix彈性客戶端
這篇文章主要介紹了了解spring中的CloudNetflix Hystrix彈性客戶端,客戶端彈性模式是在遠(yuǎn)程服務(wù)發(fā)生錯誤或表現(xiàn)不佳時保護(hù)遠(yuǎn)程資源(另一個微服務(wù)調(diào)用或者數(shù)據(jù)庫查詢)免于崩潰。,需要的朋友可以參考下2019-06-06Java多線程正確使用倒計(jì)時協(xié)調(diào)器CountDownLatch方法詳解
這篇文章主要為大家介紹了Java多線程倒計(jì)時協(xié)調(diào)器CountDownLatch的正確使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09