spring boot Invalid mime type “application/“: does not contain subtype after ‘/‘ 解决办法
does not contain subtype after ‘/’ 解决办法原因是华为摄像机报警到后台 但是华为摄像机传递的值不对, 多次联系华为都解决不了,只能平台适配了技术交流。
·
spring boot Invalid mime type
"application/"
: does not contain subtype after ‘/’ 解决办法
原因是华为摄像机报警到后台 但是华为摄像机传递的Content-Type:application/
值不对, 多次联系华为都解决不了,只能平台适配了
技术交流 http://idea.coderyj.com
1.spring boot 适配
- a 出现问题是
Content-Type
值不对,但是华为那边说更改周期比较长只能平台来解决 - b. 出现问题是代码中使用了
@RequestBody
底层会把请求体转换为json然后进行赋值,但是它的请求头不对就导致了这个问题 - 示例代码
@PostMapping("test2")
public Object test2(@RequestBody AlarmBean obj, DeviceBean deviceBean) {
log.info("接收到告警参数2" + obj + "deviceBean=" + deviceBean);
return "成功";
}
- postman
2.解决 去掉 @RequestBody
手动从HttpServletRequest
中获取请求体
- 上代码
@PostMapping("test2")
public Object test2(DeviceBean deviceBean, HttpServletRequest request) throws IOException {
ServletInputStream inputStream = request.getInputStream();
AlarmBean alarmBean = JSONObject.parseObject(inputStream, AlarmBean.class);
log.info("接收到告警参数--deviceBean " + deviceBean);
log.info("接收到告警参数" + alarmBean);
return "成功";
}
这里说一下感谢阿里的fasthjson 可以直接接收
ServletInputStream
类型 如果想转换一下也可以
- 如果不想用fastjson也可以手动解析
public String getRequestBody(HttpServletRequest request) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
try {
reader = request.getReader();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
// 处理异常
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// 处理异常
}
}
}
String requestBody = sb.toString();
// 处理请求体数据
return requestBody;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(request.getInputStream(), baos);
// 得到ByteArrayOutputStream 输出流
- AlarmBean
@Data
public class AlarmBean {
// 告警源
private String alarmSource;
// 告警名称
private String alarmName;
// 告警类型
private String commonAlarmType;
// 目标对象数组
private Object obj;
}
- DeviceBean
@Data
public class DeviceBean {
private String id;
private String deviceName;
}
手动从HttpServletRequest
中获取post请求的请求体,DeviceBean
获取url中的参数
看控制台 数据已经接收到了 到此适配
Content-Type:application/
已经完成
更多推荐
已为社区贡献7条内容
所有评论(0)