jackson使用@JsonProperty注解
来源: @JsonPrpperty是jackson包下的一个注解,详细路径 (com.fasterxml.jackson.annotation.JsonProperty;)
作用: @JsonProperty用在属性上,将属性名称序列化为另一个名称。
简单的说,就是在给实体类属性名起别名,应用在不同的场合。
jackson使用@JsonIgnore的作用
作用:在json序列化时将java 中的一些属性忽略掉,序列化和反序列化都受影响
它的作用就是在后端给前端数据的时候对后端发送出的json字符串能够发挥作用的一个注解,可以忽略不想传递给前台的属性或接口
@Data
@Accessors(chain = true)
@ApiModel("测试实体")
public class Charge implements Serializable {
@ApiModelProperty(value = "", required = true, dataType = "String")
@JsonProperty("wmid")
private String wmId;
@ApiModelProperty(value = "获取方式", required = true, dataType = "String")
@JsonProperty("gathertype")
private String gatherType;
@ApiModelProperty(value = "新区类型", required = true, dataType = "String")
@JsonProperty("invoicetype")
private String invoiceType;
@ApiModelProperty(value = "状态", required = true, dataType = "String")
@JsonIgnore
private String statue;
}
序列化以及参数输出:
//序列化
String writeValueAsString = new ObjectMapper().writeValueAsString(request);
System.out.println("=============jackson>"+writeValueAsString);
控制台输出:
"invoicetype": 1,
"wmId":"1",
"gathertype": "A",
fastjson使用@JSONField注解修改对象属性对应的序列化键的不同
@JSONField是fastjson的一个注解,在fastjson解析一个类为Json对象时,作用到类的每一个属性(field)上。 通过用@JSONField注解一个类的属性,我们可以达到以下目标 指定field对应的key名称:@JSONField(name = “NaMe”) 对于时间字段,指定其格式:@JSONField(format = “yyyy-MM-dd HH:mm:ss”) 不序列化某些字段:@JSONField(serialize = false) 指定顺序:@JSONField(ordinal = 1)
@Data
@Accessors(chain = true)
@ApiModel("测试实体")
public class Charge implements Serializable {
@ApiModelProperty(value = "", required = true, dataType = "String")
@@JSONField("wmid")
private String wmId;
@ApiModelProperty(value = "获取方式", required = true, dataType = "String")
@@JSONField("gathertype")
private String gatherType;
@ApiModelProperty(value = "新区类型", required = true, dataType = "String")
@@JSONField("invoicetype")
private String invoiceType;
}
序列化以及参数输出
//序列化
String p = JSONObject.toJSONString(param, SerializerFeature.WriteMapNullValue);
System.out.println("=============jackson>"+ p);
控制台输出:
"invoicetype": 1,
"wmid": 36482.88,
"gathertype": "A",