一、项目场景:
接口:Context-Type:text/plain
方式:POST
项目场景:硬件回调接口
二、实战
@PostMapping(value = "/xx/xxx", consumes = "text/plain" )
2.1、接口
/**
* @return String
* @time 2024年12月4日
*/
@PostMapping(value = "/xx/xxx", consumes = "text/plain")
@ApiOperation(value = "物data-接入", notes = "作者:lj")
public String djData(@RequestBody String data) throws Exception {
data = StringUtils.replace(data,"\n","");
DianjiangData djData = JSON.parseObject(data, DianjiangData.class);
log.info("【物候】点将-摄像日期:{}->{}", djData.getImgDatetime(), data);
return "OK";
}
2.2、实体类
@NoArgsConstructor
@Data
public class DianjiangData {
@ApiModelProperty(name = "cameraID", value = "相机 ID ")
@JsonProperty("cameraID")
private String cameraID;
@DateTimeFormat(pattern = "yyyyMMddHHmmss")
@JsonFormat(pattern = "yyyyMMddHHmmss", timezone = "UTC+8")
@ApiModelProperty(name = "imgDatetime", value = "摄像日期时间")
@JsonProperty("imgDatetime")
private Date imgDatetime;
@ApiModelProperty(name = "meanNDVI", value = "归一化植被指数,范围[-1,1],保留8位小数")
@JsonProperty("meanNDVI")
private Double meanNDVI;
@ApiModelProperty(name = "roiNdvi", value = "中心区域归一化植被指数,范围[-1,1],保留8位小数")
@JsonProperty("roiNdvi")
private Double roiNdvi;
//注:图片上传名称规则:rgb图片名称以[***-rgb.jpg/jpeg]结尾,近红外图片名称以[***-nir.jpg/jpeg]结尾
@ApiModelProperty(name = "name", value = "图像名称")
@JsonProperty("name")
private String name;
@ApiModelProperty(name = "gcc", value = "GCC 值-物候指数,范围[-1,1],保留8位小数")
@JsonProperty("gcc")
private Double gcc;
@ApiModelProperty(name = "gvi", value = "GVI 值-物候指数,范围[-1,1],保留8位小数")
@JsonProperty("gvi")
private Double gvi;
@ApiModelProperty(name = "rcc", value = "相对红度指数,范围[-1,1],保留8位小数")
@JsonProperty("rcc")
private Double rcc;
@ApiModelProperty(name = "bcc", value = "相对蓝度指数,范围[-1,1],保留8位小数")
@JsonProperty("bcc")
private Double bcc;
@DateTimeFormat(pattern = "yyyyMMddHHmmss")
@JsonFormat(pattern = "yyyyMMddHHmmss", timezone = "UTC+8")
@ApiModelProperty(name = "exposure_time", value = "曝光时间")
@JsonProperty("exposure_time")
private Date exposureTime;
@ApiModelProperty(name = "fileSize", value = "图像大小,单位 KB")
@JsonProperty("fileSize")
private Long fileSize;
}
三、常见问题:
1、springboot拦截器引起的错误:Content type ‘text/plain;charset=UTF-8’ not supported
1.1、原因分析
整合了springboot拦截器
public class WebConfig implements WebMvcConfigurer {
/**
* 添加自定义会覆盖默认转换器
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 将定义的时间格式转换器添加到转换器列表中,这样jackson格式化时候但凡遇到Date类型就会转换成定义的格式
converters.add(jackson2HttpMessageConverter());
}
public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
// 包含空字段
// mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 输出时间格式
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
// 输出Long格式为String
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
mapper.registerModule(simpleModule);
converter.setObjectMapper(mapper);
// converter.setSupportedMediaTypes(getSupportedMediaTypes());
return converter;
}
}
1.2、解决方案:
1、简单明了的解决办法就是,不要覆盖原来的序列化器,而是继承并添加
/**
* 添加自定义消息转换器不覆盖默认转换器
* @param converters
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
System.out.println(converters.toString());
// 将定义的时间格式转换器添加到转换器列表中,这样jackson格式化时候但凡遇到Date类型就会转换成定义的格式
converters.add(jackson2HttpMessageConverter());
}
2、精准定位的解决办法是:添加相应的序列化器
如添加上述的:org.springframework.http.converter.StringHttpMessageConverter@b2e4d8
/**
* 添加自定义会覆盖默认转换器
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 将定义的时间格式转换器添加到转换器列表中,这样jackson格式化时候但凡遇到Date类型就会转换成定义的格式
converters.add(jackson2HttpMessageConverter());
// 添加字符串序列化器
converters.add(new StringHttpMessageConverter());
}
参考: springboot拦截器引起的错误