Java 奇怪的jackson对象映射器行为
我的Java应用程序在接收SpotifyAPI验证令牌并将它们映射到Jackson的POJO时遇到了问题 每次我的应用程序从Spotify API请求数据时,我都会从以下链接获取一个新的访问令牌: 答案是JSON,如下所示:Java 奇怪的jackson对象映射器行为,java,json,jackson,Java,Json,Jackson,我的Java应用程序在接收SpotifyAPI验证令牌并将它们映射到Jackson的POJO时遇到了问题 每次我的应用程序从Spotify API请求数据时,我都会从以下链接获取一个新的访问令牌: 答案是JSON,如下所示: { "access_token":"BQAJmzZOdh2egvWEOEwy4wv-VKdhTUc4eZYJrIfAibjWLR4MPfrbV6KBNIiomPwJKsQN-3vmrGmG7lOXFaI", "
{
"access_token":"BQAJmzZOdh2egvWEOEwy4wv-VKdhTUc4eZYJrIfAibjWLR4MPfrbV6KBNIiomPwJKsQN-3vmrGmG7lOXFaI",
"token_type":"Bearer",
"expires_in":3600,
"scope":""
}
每次我启动我的应用程序时,它第一次运行良好,但随后崩溃:
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.slowedandthrowed.darkjazzbot.mapping.spotify.TokenRequest` (although at least
one Creator exists): no String-argument constructor/factory method to deserialize from String value ('BQCcDMOKUiVPscDrrBH77b2QbN9FuqjAJHuM3_1QD39MO9L20XzXneZUlJeIyukBVhPpaCWnKWRjUdggaCM') at [Source: (String)"{"access_token":"BQCcDMOKUiVPscDrrBH77b2QbN9FuqjAJHuM3_1QD39MO9L20XzXneZUlJeIyukBVhPpaCWnKWRjUdggaCM","token_type":"Bearer","expires_in":3600,"scope":""}"; line: 1, column: 17]
每次我需要将access token JSON映射到POJO时,我都会创建一个新的ObjectMapper来解决这个问题,但如果它是一个生产应用程序,它会损害性能,因此我需要找出始终使用一个ObjectMapper实例的问题所在
我还尝试将这个JSON映射到map,而不是将它映射到TokenRequest.class
,结果是一样的,所以我认为这不是映射失败的原因
private String requestAccessToken() throws IOException {
TokenRequest tokenRequest = null;
URL accessTokenUrl = new URL(SPOTIFY_TOKEN_LINK);
HttpURLConnection tokenConnection = (HttpURLConnection) accessTokenUrl.openConnection();
tokenConnection.setRequestProperty("Authorization", authString);
tokenConnection.setDoOutput(true);
tokenConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
tokenConnection.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(tokenConnection.getOutputStream());
wr.write(TOKEN_REQUEST_PARAMETERS);
wr.flush();
System.out.println("Wow! Posted!");
InputStream inputStream = tokenConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder inputBuilder = new StringBuilder();
String input = null;
while ((input = reader.readLine()) != null) inputBuilder.append(input);
System.out.println("================================= TOKEN INPUT ======================================");
System.out.println(inputBuilder.toString());
System.out.println("================================= TOKEN INPUT ======================================");
tokenRequest = spotifyObjectMapper.readValue(inputBuilder.toString(), TokenRequest.class);
inputStream.close();
reader.close();
tokenConnection.disconnect();
return tokenRequest.getAccessToken();
}
TokenRequest.java:
package com.slowedandthrowed.darkjazzbot.mapping.spotify;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonRootName;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"access_token",
"token_type",
"expires_in",
"scope"
})
@JsonRootName("access_token")
public class TokenRequest {
@JsonProperty("access_token")
private String accessToken;
@JsonProperty("token_type")
private String tokenType;
@JsonProperty("expires_in")
private Long expiresIn;
@JsonProperty("scope")
private String scope;
@JsonProperty("access_token")
public String getAccessToken() {
return accessToken;
}
@JsonProperty("access_token")
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
@JsonProperty("token_type")
public String getTokenType() {
return tokenType;
}
@JsonProperty("token_type")
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
@JsonProperty("expires_in")
public Long getExpiresIn() {
return expiresIn;
}
@JsonProperty("expires_in")
public void setExpiresIn(Long expiresIn) {
this.expiresIn = expiresIn;
}
@JsonProperty("scope")
public String getScope() {
return scope;
}
@JsonProperty("scope")
public void setScope(String scope) {
this.scope = scope;
}
}
我修复了我的程序,因为我为自己制造了问题。 我通过不同的API接收不同的数据,并通过Jackson ObjectMapper的单个实例将其映射到POJO。某些JSON对象本身包含数据:
{
"property1":value1,
"property2":value2
}
而另一个具有单个嵌套对象,其中包含数据:
{
"object":{"property1":value1,
"property2":value2
}}
因此,我决定将“展开根”值设置为TRUE,然后我的对象映射器更改了其状态,使其行为在我第一次和第二次尝试接收访问令牌时有所不同。我修复了我的程序,因为我为自己制造了问题。 我通过不同的API接收不同的数据,并通过Jackson ObjectMapper的单个实例将其映射到POJO。某些JSON对象本身包含数据:
{
"property1":value1,
"property2":value2
}
而另一个具有单个嵌套对象,其中包含数据:
{
"object":{"property1":value1,
"property2":value2
}}
因此,我决定将“展开根”值设置为TRUE,然后我的对象映射器更改了其状态,使其行为在我第一次和第二次尝试接收访问令牌时有所不同。为什么有
@JsonRootName
?另外请注意,您可以将@JsonNaming(PropertyNamingStrategy.snakecasetrantegy.class)
添加到类中,并删除处理属性的所有其他注释。这只是尝试修复应用程序行为。我的问题是打开UNWRAP\u ROOT\u值并忘记了它,所以我看到了一个关于“预期访问\u令牌类名”的异常。非常感谢。现在,我删除了过多的注释,更好地理解了映射,并修复了总体行为。为什么有@JsonRootName
?另外请注意,您可以将@JsonNaming(PropertyNamingStrategy.snakecasetrantegy.class)
添加到类中,并删除处理属性的所有其他注释。这只是尝试修复应用程序行为。我的问题是打开UNWRAP\u ROOT\u值并忘记了它,所以我看到了一个关于“预期访问\u令牌类名”的异常。非常感谢。现在,我删除了过多的注释,更好地理解了映射,并修复了总体行为。