我有JSON格式的输入,例如
{
"msisdn" : "<some value>",
"currentStackType" : "<some value>",
"msisdnDestination" : "<some value>",
"ncMigrationStatus" : "<some value>",
"spid" : "<some value>",
"ncMigrationTimestamp" : "2019-01-16 18:04:26"
}
我必须反序列化的类中的提到的JSON数据字段的名称与对应的JSON字段的名称相同。我也使用@JsonProperty
注释,因为没有它们,“ currentStackType”,“ msisdnDestination”和
ObjectMapper无法识别“ ncMigrationStatus”。
致电ObjectMapper.readValue(json, <MyClassName>.class)
后,我注意到反序列化成功。
但是在此之后,出于记录目的,我执行相反的操作并遵守以下规定:
{
"migrationStatus": "<some value>",
"destination": "<some value>",
"migrationTimestamp": "<some value>",
"stackType": "<some value>",
"msisdn": "<some value>",
"currentStackType": "<some value>",
"msisdnDestination": "<some value>",
"ncMigrationStatus": "<some value>",
"ncMigrationTimestamp": "2019-01-16 06:04:26",
"spid": "<some value>"
}
所有以小驼色命名的字段实际上都使用“精简”名称重复。
除了重命名JSON属性之外,还有其他方法可以消除这种情况吗?
我尝试直接设置PropertyNamingStrategy
,但没有任何改变。
我的课看起来像
public class ******* {
@JsonProperty("msisdn")
@NotBlank(message = "'MSISDN' should not be blank")
private String msisdn;
@JsonProperty("currentStackType")
@NotBlank(message = "'Current Stack Type' should not be blank")
private String currentStackType;
@JsonProperty("msisdnDestination")
@NotBlank(message = "'MSISDN Destination' should not be blank")
private String msisdnDestination;
@JsonProperty("ncMigrationStatus")
@NotBlank(message = "'NC Migration Status' should not be blank")
private String ncMigrationStatus;
@JsonProperty("ncMigrationTimestamp")
@NotNull(message = "'NC Migration Timestamp' should not be null")
private Date ncMigrationTimestamp;
@JsonProperty("spid")
@NotBlank(message = "'SPID' should not be blank")
private String spid;
public *******() {
}
public void setMSISDN(String val) {
msisdn = val;
}
public String getMSISDN() {
return msisdn;
}
public void setStackType(String val) {
currentStackType = val;
}
public String getStackType() {
return currentStackType;
}
public void setDestination(String val) {
msisdnDestination = val;
}
public String getDestination() {
return msisdnDestination;
}
public void setMigrationStatus(String val) {
ncMigrationStatus = val;
}
public String getMigrationStatus() {
return ncMigrationStatus;
}
public void setMigrationTimestamp(Date val) {
ncMigrationTimestamp = val;
}
public Date getMigrationTimestamp() {
return ncMigrationTimestamp;
}
public void setSPID(String val) {
spid = val;
}
public String getSPID() {
return spid;
}
}
对不起,我必须隐藏一些细节。
答案 0 :(得分:0)
我找到了根本原因:setter和getter方法的名称必须与模式匹配
<set/get><full name of corresponding class data member>
。
驼色大小写也必须匹配,唯一的例外是:set/get
单词后的首字母必须大写。也许可以使用注释来应用一些灵活性,但是我还没有尝试过。