@JSONField 它在 spring 引导中不起作用
@JSONField it did not work in spring boot
我对pointX和pointY使用了@JSONField注解,我想把pointX改成point_x!
我要pointX show like point_x!但它没有用!它始终是 pointX 和 pointY!但其他字段没问题!
我的java豆子是这样的!
@Data
public class PointListOutput {
@JSONField(name = "latest_area_point_list")
private List<Point> latestAreaPointList;
@Data
public static class Point {
@JSONField(name = "point_x")
private Float pointX;
@JSONField(name = "point_y")
private Float pointY;
}
}
我的配置
@Override
protected void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
// create FastJson converter
final FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
final List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(supportedMediaTypes);
// create configuration class
final FastJsonConfig config = new FastJsonConfig();
// update filter of response
config.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
converter.setFastJsonConfig(config);
converters.add(new ByteArrayHttpMessageConverter());
converters.add(converter);
}
这是我的 http 响应,你可以看到 pointX 是 pointX!不是 point_x!
{
"code": 1,
"message": "Success",
"data": [
{
"latest_area_point_list": [
{
"pointX": 1.1,
"pointY": 1.1
},
{
"pointX": null,
"pointY": 1.1
}
]
}
]
}
我解决了这个问题,原因是 org.springframework.beans.BeanUtils.copyProperties
我使用 org.springframework.beans.BeanUtils.copyProperties
将 DAO java bean 更改为 View java bean。
我的 Dao java bean
@Data
@DynamoDBTable(tableName = "xxxx")
public class PointPojo {
@DynamoDBAttribute(attributeName = "latest_area_point_list")
private List<Point> latestAreaPointList;
@Data
@DynamoDBDocument
public static class Point {
@DynamoDBAttribute(attributeName = "point_x")
private Float pointX;
@DynamoDBAttribute(attributeName = "point_y")
private Float pointY;
}
}
而PointPojo
没有哈瓦@JSONField(name = "point_x")
在 copyProperties
之后 Point
不是 PointListOutput$Point
,而是 PointPojo@Point
,并且没有 @JSONField(name = "point_x")
所以这是根本原因!
我用orika代替了org.springframework.beans.BeanUtils.copyProperties
,完美!
我对pointX和pointY使用了@JSONField注解,我想把pointX改成point_x! 我要pointX show like point_x!但它没有用!它始终是 pointX 和 pointY!但其他字段没问题!
我的java豆子是这样的!
@Data
public class PointListOutput {
@JSONField(name = "latest_area_point_list")
private List<Point> latestAreaPointList;
@Data
public static class Point {
@JSONField(name = "point_x")
private Float pointX;
@JSONField(name = "point_y")
private Float pointY;
}
}
我的配置
@Override
protected void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
// create FastJson converter
final FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
final List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(supportedMediaTypes);
// create configuration class
final FastJsonConfig config = new FastJsonConfig();
// update filter of response
config.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
converter.setFastJsonConfig(config);
converters.add(new ByteArrayHttpMessageConverter());
converters.add(converter);
}
这是我的 http 响应,你可以看到 pointX 是 pointX!不是 point_x!
{
"code": 1,
"message": "Success",
"data": [
{
"latest_area_point_list": [
{
"pointX": 1.1,
"pointY": 1.1
},
{
"pointX": null,
"pointY": 1.1
}
]
}
]
}
我解决了这个问题,原因是 org.springframework.beans.BeanUtils.copyProperties
我使用 org.springframework.beans.BeanUtils.copyProperties
将 DAO java bean 更改为 View java bean。
我的 Dao java bean
@Data
@DynamoDBTable(tableName = "xxxx")
public class PointPojo {
@DynamoDBAttribute(attributeName = "latest_area_point_list")
private List<Point> latestAreaPointList;
@Data
@DynamoDBDocument
public static class Point {
@DynamoDBAttribute(attributeName = "point_x")
private Float pointX;
@DynamoDBAttribute(attributeName = "point_y")
private Float pointY;
}
}
而PointPojo
没有哈瓦@JSONField(name = "point_x")
在 copyProperties
之后 Point
不是 PointListOutput$Point
,而是 PointPojo@Point
,并且没有 @JSONField(name = "point_x")
所以这是根本原因!
我用orika代替了org.springframework.beans.BeanUtils.copyProperties
,完美!