SpringBoot @PostMapping接收HTTP請(qǐng)求的流數(shù)據(jù)問(wèn)題
@PostMapping接收HTTP請(qǐng)求的流數(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 請(qǐng)求
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對(duì)象
//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請(qǐng)求
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
JSONObject data = new JSONObject();
data.element("key", "這是一條測(cè)試數(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();
// 斷開(kāi)連接
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注解解析
開(kāi)發(fā)過(guò)程IDEA提示如將@RequestMapping(value="/abc" , method = “RequestMethod.POST”)替換成@PostMapping。現(xiàn)對(duì)@PostMapping的實(shí)現(xiàn)。
@PostMapping是一個(gè)復(fù)合注解,Spring framework 4.3引入了@RequestMapping注釋的變體,以更好地表示帶注釋的方法的語(yǔ)義,作為@RequestMapping(method = RequestMethod.POST)的快捷方式。
也就是可以簡(jiǎn)化成@PostMapping(value="/abc" )即可,主要是方便識(shí)記。
下面很多方法都是對(duì)應(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)為空字符串,一般需要自己填寫(xiě)
*/
@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)類(lèi)似
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 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類(lèi)型參數(shù)多兩個(gè)“引號(hào)問(wèn)題
相關(guān)文章
mybatisplus 的SQL攔截器實(shí)現(xiàn)關(guān)聯(lián)查詢(xún)功能
大家都知道m(xù)ybatisplus不支持關(guān)聯(lián)查詢(xún),后來(lái)學(xué)習(xí)研究發(fā)現(xiàn)mybatisplus的SQL攔截器可以實(shí)現(xiàn)這一操作,下面小編給大家分享我的demo實(shí)現(xiàn)基本的關(guān)聯(lián)查詢(xún)功能沒(méi)有問(wèn)題,對(duì)mybatisplus關(guān)聯(lián)查詢(xún)相關(guān)知識(shí)感興趣的朋友一起看看吧2021-06-06
java中構(gòu)造方法及this關(guān)鍵字的用法實(shí)例詳解(超詳細(xì))
大家都知道,java作為一門(mén)內(nèi)容豐富的編程語(yǔ)言,其中涉及的范圍是十分廣闊的,下面這篇文章主要給大家介紹了關(guān)于java中構(gòu)造方法及this關(guān)鍵字用法的相關(guān)資料,需要的朋友可以參考下2022-04-04
kafka-console-consumer.sh使用2次grep管道無(wú)法提取消息的解決
這篇文章主要介紹了kafka-console-consumer.sh使用2次grep管道無(wú)法提取消息的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
運(yùn)用springboot搭建并部署web項(xiàng)目的示例
這篇文章主要介紹了運(yùn)用springboot搭建并部署web項(xiàng)目的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
通過(guò)反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解
這篇文章主要介紹了通過(guò)反射實(shí)現(xiàn)Java下的委托機(jī)制代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
了解spring中的CloudNetflix Hystrix彈性客戶(hù)端
這篇文章主要介紹了了解spring中的CloudNetflix Hystrix彈性客戶(hù)端,客戶(hù)端彈性模式是在遠(yuǎn)程服務(wù)發(fā)生錯(cuò)誤或表現(xiàn)不佳時(shí)保護(hù)遠(yuǎn)程資源(另一個(gè)微服務(wù)調(diào)用或者數(shù)據(jù)庫(kù)查詢(xún))免于崩潰。,需要的朋友可以參考下2019-06-06
Java多線程正確使用倒計(jì)時(shí)協(xié)調(diào)器CountDownLatch方法詳解
這篇文章主要為大家介紹了Java多線程倒計(jì)時(shí)協(xié)調(diào)器CountDownLatch的正確使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

