我不清楚Jackson是如何处理Map字段中的大写的。如果有人能帮助我,我将不胜感激。
{"user":{"username":"user@host.com","password":"pwd","sendercompid":"COMPID","service":{"host":"address","port":6666,"service":"S1","serviceAsString":"s1"}},"MDReqID":"ghost30022","NoRelatedSym":1,"Symbol":["GOOG"],"MarketDepth":"0","NoMDEntryTypes":3,"MDEntryType":["0","1","2"],"SubscriptionRequestType":"1","AggregatedBook":"N"}:
上面是我的json,下面是我的例外......
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "MDReqID" (class com.myco.qa.fixrest.MarketDataRequest), not marked as ignorable (10 known properties: , "mdreqID", "marketDepth", "user", "subscriptionRequestType", "aggregatedBook", "mdentryType", "symbol", "mdupdateType", "noRelatedSym", "noMDEntryTypes"])
上面是我的例外,下面是我的班级......
public class MarketDataRequest {
private User user;
private String MDReqID;
private char SubscriptionRequestType;
private int MarketDepth;
private int MDUpdateType;
private char AggregatedBook;
private int NoMDEntryTypes;
private ArrayList<Character> MDEntryType;
private int NoRelatedSym;
private ArrayList<String> Symbol;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getMDReqID() {
return MDReqID;
}
public void setMDReqID(String MDReqID) {
this.MDReqID = MDReqID;
}
public char getSubscriptionRequestType() {
return SubscriptionRequestType;
}
public void setSubscriptionRequestType(char subscriptionRequestType) {
SubscriptionRequestType = subscriptionRequestType;
}
等等
5条答案
按热度按时间eit6fx6z1#
由于setter方法命名为
setMDReqID(…)
,Jackson假定该变量命名为mDReqID
,这是因为Java * 命名约定 *(变量应以小写字母开头).如果您确实需要大写字母,请在 setter 上使用@JsonProperty注解(或者,对于序列化,请在 getter 上使用),如下所示:
ecbunoof2#
你也可以做
在类上,将JSON消息中的所有属性名称大写
mqkwyuun3#
在setter上添加与接收到的JSON字符串中的属性名称相匹配的
@JsonProperty
:此外,将
@JsonProperty
注解添加到 getter 中,以使输出以 * 常规 * 格式显示:现在,您可以随意命名变量:
xggvc2p64#
我解决这个问题的方法是:
输出:
fbcarpbf5#
我遇到了同样的问题,在尝试了
UpperCamelCaseStrategy
之后,仍然出现了这个错误,该策略使我的字段pContent
变为ObjectMapper属性Pcontent
,因为不想为每个字段添加@JsonProperty,最后简单地使用gson
代替