Java Jackson ObjectMapper readValue()解析对象时无法识别字段

Java Jackson ObjectMapper readValue()解析对象时无法识别字段,java,json,rest,jackson,objectmapper,Java,Json,Rest,Jackson,Objectmapper,我正在用Java/Spring创建简单的rest客户端。我的请求已被远程服务正确使用,我得到了响应字符串某物: {"access_token":"d1c9ae1b-bf21-4b87-89be-262f6","token_type":"bearer","expires_in":43199,"grant_type":"client_credentials"} 下面的代码是我想要从Json响应中绑定值的对象 package Zadanie2.Zadanie2; import com.faste

我正在用Java/Spring创建简单的rest客户端。我的请求已被远程服务正确使用,我得到了响应字符串某物:

{"access_token":"d1c9ae1b-bf21-4b87-89be-262f6","token_type":"bearer","expires_in":43199,"grant_type":"client_credentials"}

下面的代码是我想要从Json响应中绑定值的对象

package Zadanie2.Zadanie2;

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

 public class Token {
String access_token;        
String token_type;
int expiresIn;
String grantType;
//////////////////////////////////////////////////////
public Token() {
    /////////////////////////////////
}
/////////////////////////////////////////////////////

public void setAccessToken(String access_token) {
    this.access_token=access_token;
}
public String getAccessToken() {
    return access_token;
}
////////////////////////////////////////////////
public void setTokenType(String token_type) {
    this.token_type=token_type;
}
public String getTokenType() {
    return token_type;
}
//////////////////////////////////////////////////////
public void setExpiresIn(int expiresIn) {
    this.expiresIn=expiresIn;
}
public int getExpiresIn() {
    return expiresIn;
}
//////////////////////////////////////////////////////////
public void setGrantType(String grantType) {
    this.grantType=grantType;
}
public String getGrantType() {
    return grantType;
}
}
一直以来,我都在获取“无法识别的字段访问\u令牌”,但当我添加
objectMapper.configure(在\u未知\u属性上反序列化feature.FAIL\u,false)时
那么访问令牌将为空

        jsonAnswer=template.postForObject(baseUriAuthorize, requestEntity,  String.class);  
        System.out.println(jsonAnswer);
        Token token=objectMapper.readValue(jsonAnswer, Token.class);
        System.out.println(token.getAccessToken());

我尝试了
@JsonProperty
注释。例如,我尝试通过
“@JsonProperty(accessToken)”
更改字段,因为我认为变量名中的“\ux”符号存在问题。我加入了接球手和二传手。我使用的版本可能有问题,但我不这么认为,因为我使用的是
“com.fasterxml.jackson.core”

您的setter与JSON密钥不匹配

要正确阅读,应将设置器更改为:

setAccess_token()
setToken_type()
...
但老实说,这太难看了

尝试遵循JavaBean名称约定,并使用
@JsonProperty
自定义JSON键:

public class Token {
        @JsonProperty("access_token")
        String accessToken;
         ....

}
您尝试了
“@JsonProperty(accessToken)”
。但是您的json包含
access\u令牌
。它是如何工作的? 尝试使用此类:

public class Token {
    @JsonProperty("access_token")
    String accessToken;
    @JsonProperty("token_type")
    String tokenType;
    int expiresIn;
    String grantType;
    //getter setter
}

getters setter不遵循您的名称约定。因此,您可以使用getToken_type()作为令牌_类型,也可以使用JsonProperty注释并使用驼峰大小写getter和setter。只需遵守Java命名约定,并使用JsonProperty注释字段,以指定其json键名称。可能它不受支持,因为当我使用@JsonProperty修改类时,它不起作用,除非应该删除setters/getter??您还必须将字段更改为
accessToken
(下划线已删除)。