您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot @PostMapping接收HTTP請求的流數(shù)據(jù)問題怎么解決”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SpringBoot @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(); }
開發(fā)過程IDEA提示如將@RequestMapping(value="/abc" , method = “RequestMethod.POST”)替換成@PostMapping。現(xiàn)對@PostMapping的實現(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實現(xiàn)類似
感謝各位的閱讀,以上就是“SpringBoot @PostMapping接收HTTP請求的流數(shù)據(jù)問題怎么解決”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對SpringBoot @PostMapping接收HTTP請求的流數(shù)據(jù)問題怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。