注意:我知道变量的名称不正确。我正在清理另一个开发人员的旧代码,他将这些代码列为成员变量。他们将被正确命名,是的,我知道我需要注解,但我在这里要找的是一个解释,为什么变量是employeeid,它在寻找employeeid,但找到一个不存在的变量memployeeid。顺便说一句,这种情况发生在服务器上,而不仅仅是在ide中。
出于某种未知的原因-在修改变量名、保存、重新启动、使缓存失效等之后-jackson无法Map到propper变量名。为此,我使用intellij和java。
错误:
Exception in badge service getting by badgeId: Unrecognized field
"EmployeeId" (class com.companyName.attendance.entity.DTOs.badgeservice.BadgeDTO), not marked as ignorable (6 known properties: "mBadgeId", "mEmployeeId", "mLanId", "mFirstName", "mEmail", "mLastName"])
at [Source: (String)"{"backoff":null,"error_id":null,"error_message":null,"error_name":null,"has_more":false,"items":[{"EmployeeId":"888888","LanId":"NTest","FirstName":"Name","MiddleName":null,"LastName":"Test","Email":null,"Location":null,"Title":null,"Phone":null,"DepartmentId":null,"DepartmentName":null,"DepartmentNumber":null,"Groups":null,"Found":false,"BadgeId":"222222","JobTitle":null,"Picture":null,"OrgUrl":null,"Manager":null,"Coworkers":null,"DirectReports":null}],"page":null,"page_size":null,"quo"[truncated 61 chars]; line: 1, column: 113] (through reference chain: com.companyName.attendance.entity.DTOs.badgeservice.BadgeServiceResponseDTO["items"]->java.util.ArrayList[0]->com.companyName.attendance.entity.DTOs.badgeservice.BadgeDTO["EmployeeId"])
如您所见,它没有在dto中找到employeeid,而是说期望的feld是memployeeid。但是,下面是我的dto变量声明:
public class BadgeDTO {
//TODO: Convert member variables to proper practice names
String BadgeId;
String EmployeeId;
String FirstName;
String LanId;
String LastName;
String Email;
public BadgeDTO(String BadgeId, String EmployeeId, String FirstName,
String LanId, String LastName, String Email) {
super();
this.BadgeId = BadgeId;
this.EmployeeId = EmployeeId;
this.FirstName = FirstName;
this.LanId = LanId;
this.LastName = LastName;
this.Email = Email;
}
public BadgeDTO() {
super();
}
public String getmBadgeId() {
return BadgeId;
}
public void setmBadgeId(String BadgeId) {
this.BadgeId = BadgeId;
}
public String getmEmployeeId() {
return EmployeeId;
}
public void setmEmployeeId(String EmployeeId) {
this.EmployeeId = EmployeeId;
}
public String getmFirstname() {
return FirstName;
}
public void setmFirstName(String FirstName) {
this.FirstName = FirstName;
}
public String getmLanId() {
return LanId;
}
public void setmLanId(String LanId) {
this.LanId = LanId;
}
public String getmLastName() {
return LastName;
}
public void setmLastName(String LastName) {
this.LastName = LastName;
}
public String getmEmail() {
return Email;
}
public void setmEmail(String Email) {
this.Email = Email;
}
@Override
public String toString() {
return "BadgeDTO [BadgeId=" + BadgeId + ", EmployeeId=" + EmployeeId +
", Firstname=" + FirstName
+ ", LanId=" + LanId + ", LastName=" + LastName + ", Email=" +
Email + "]";
}
}
现在最疯狂的是,做以下工作:
@JsonProperty("BadgeId")
String BadgeId;
@JsonProperty("EmployeeId")
String EmployeeId;
@JsonProperty("FirstName")
String FirstName;
json码:
{
"backoff": null,
"error_id": null,
"error_message": null,
"error_name": null,
"has_more": false,
"items": [
{
"EmployeeId": "888888",
"LanId": "TName",
"FirstName": "Test",
"MiddleName": null,
"LastName": "Name",
"Email": null,
"Location": null,
"Title": null,
"Phone": null,
"DepartmentId": null,
"DepartmentName": null,
"DepartmentNumber": null,
"Groups": null,
"Found": false,
"BadgeId": "222222",
"JobTitle": null,
"Picture": null,
"OrgUrl": null,
"Manager": null,
"Coworkers": null,
"DirectReports": null
}
],
"page": null,
"page_size": null,
"quota_max": null,
"quota_remaining": null,
"total": null,
"type": null
}
因此,我已经使用上面的jsonproperty注解解决了这个问题,但是为什么它仍然在寻找memployeeid,而它在我的整个代码中不存在呢?我想失效/重启可以解决这个问题,但它没有
2条答案
按热度按时间6qftjkof1#
答案通常是:注意你写的代码。
getter和setter将字段公开为m*(其中*是实际的字段名)。
看看这个:
这是字母“m”。
因此,这将字段名公开为“memployeeid”
更多详细信息:
方法返回的值与getter公开的值的名称无关。java要求getter名称的格式为“getfieldname”,setter nanes的格式为“setfieldname”,其中“fieldname”是任何不是方法名的“get”或“set”部分的值。
这被称为“javabean命名约定”,如果您使用java编写代码并使用任何第三方java库,那么您必须理解并遵守它。
ff29svar2#
匹配
JSON
钥匙POJO
属性Jackson
使用一种叫做PropertyNamingStrategy
. 在你的JSON
我们至少可以找到两种策略:蛇壳(
page_size
,error_message
等)上\u Camel \u箱(
EmployeeId
,DepartmentId
等)从另一边
POJO
类提供第三种策略:“m”+大写\u(
mEmployeeId
,mDepartmentId
等)这就是为什么
JSON
不匹配POJO
. 要使其发挥作用,您需要实施如下所示的新战略:您可以这样使用它:
从现在起,您可以反序列化上面的内容
JSON
给予POJO
.当您添加
@JsonProperty
带有已告知的属性名称的注解Jackson
使用自定义Map。另请参见:
Spring Jackson酒店命名策略
物业管理策略
更多Jackson注解