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)文章
Spring?Boot如何通過Actuator顯示git和build的信息
這篇文章主要介紹了Spring?Boot通過Actuator顯示git和build的信息,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01
java 多線程實(shí)現(xiàn)在線咨詢(udp)
這篇文章主要介紹了java 多線程實(shí)現(xiàn)在線咨詢(udp)的示例,幫助大家更好的理解和學(xué)習(xí)Java 網(wǎng)絡(luò)編程的相關(guān)內(nèi)容,感興趣的朋友可以了解下2020-11-11
Java連接MySQL數(shù)據(jù)庫增刪改查的通用方法(推薦)
下面小編就為大家?guī)硪黄狫ava連接MySQL數(shù)據(jù)庫增刪改查的通用方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Springboot前后端分離項(xiàng)目配置跨域?qū)崿F(xiàn)過程解析
這篇文章主要介紹了Springboot前后端分離項(xiàng)目配置跨域?qū)崿F(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
java實(shí)現(xiàn)zip,gzip,7z,zlib格式的壓縮打包
本文是利用Java原生類和apache的commons實(shí)現(xiàn)zip,gzip,7z,zlib的壓縮打包,如果你要是感興趣可以進(jìn)來了解一下。2016-10-10
Spring Boot 2.0.0 終于正式發(fā)布-重大修訂版本
北京時間 2018 年 3 月 1 日早上,如約發(fā)布的 Spring Boot 2.0 在同步至 Maven 倉庫時出現(xiàn)問題,導(dǎo)致在 GitHub 上發(fā)布的 v2.0.0.RELEASE 被撤回2018-03-03

