java实体转json,字段保持大写

java实体转json,字段首字母保持大写

1、在实体类的字段加上注解及 get方法上加注解

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Image{
	....
    @JsonProperty("ImageID")
    private String ImageID;
    
    @JsonIgnore
    public String getImageID() {
        return ImageID;
    }

    public void setImageID(String imageID) {
        ImageID = imageID;
    }
}

2、在实体转json时, 使用jackson

import com.fasterxml.jackson.databind.ObjectMapper;
			......
            ObjectMapper mapper = new ObjectMapper();
            String s = mapper.writeValueAsString(Image);
            System.out.println(s)
            .....

3、结果实例

 {
                    "ImageID": "xxxxxxxxxx",
                    "EventSort": "10",
                    "DeviceID": "xxxxxxxx",
                    "StoragePath": "xxxxxxxxxx",
                    "StoragePath2": null,
                    "Type": "14",
                    "FileFormat": "Jpeg",
                    "ShotTime": "20210527104001",
                    "Width": 2688,
                    "Height": 1520,
                    "Data": null
},

版权声明:本文为weixin_44149422原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。