1

我对 pointX 和 pointY 使用@JSONField 注释,我想将 pointX 更改为 point_x!我想像point_x一样显示pointX!但它没有用!它始终是 pointX 和 pointY!但其他领域还可以!

我的java bean是这样的!

@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
                }
            ]
        }
    ]
}
4

1 回答 1

0

我解决了这个问题,这是因为org.springframework.beans.BeanUtils.copyProperties
我使用org.springframework.beans.BeanUtils.copyProperties 将 DAO java bean 更改为 View java bean。
我的道 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;
    }
}

在was not 之后PointPojo没有 hava ,它是,它没有 hava 所以这就是根本原因!@JSONField(name = "point_x")
copyPropertiesPointPointListOutput$PointPointPojo@Point@JSONField(name = "point_x")

我用orika replace org.springframework.beans.BeanUtils.copyProperties,很完美!</p>

于 2021-05-24T05:43:32.897 回答