SpringBoot@PostMapping接收HTTP请求的流数据问题
@PostMapping接收HTTP请求的流数据
@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 {
//创建连接
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数据
//application/x-javascript->json对象
//application/x-www-fORM-urlencoded->表单数据
//application/json;charset=utf-8 -> json数据
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("accept", "*
@AliasFor(annotation = RequestMapping.class)
String name() default "";
@AliasFor(annotation = RequestMapping.class)
String[] value() default {};
@AliasFor(annotation = RequestMapping.class)
String[] path() default {};
@AliasFor(annotation = RequestMapping.class)
String[] params() default {};
@AliasFor(annotation = RequestMapping.class)
String[] headers() default {};
@AliasFor(annotation = RequestMapping.class)
String[] consumes() default {};
@AliasFor(annotation = RequestMapping.class)
String[] produces() default {};
}
其他变体如下:
@GetMapping、@PutMapping、@PatchMapping和@DeleteMapping,与@PostMapping实现类似
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
相关文章