解码后将base64令牌转换为jsonString



我正在尝试将json字符串转换为我各自的pojo类。json字符串是base64令牌的解码版本
解码值为

{"userId":"1234567890","userName": "John Doe", roles: ["admin","users"]}

我使用代码String jsonFormat=objectMapper.writeValueAsString(decoded);,其中解码的是上面提到的字符串。

当我试图使用objectmapper将其转换为对象时,我得到了以下错误

objectMapper.readValue(jsonFormat, PtsbUser.class);

错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.ptsb.rbaccomponent.models.PtsbUser` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{userId:"1234567890",userName: "John Doe", roles: ["admin","users"]}')
at [Source: (String)""{userId:"1234567890",userName: "John Doe", roles: ["admin","users"]}n     n  n    ""; line: 1, column: 1]

我已经更新了解码字符串,把括号也放在键中。我正在使用https://www.base64encode.org/对于给我代币的编码base64

eyJ1c2VySWQiOiIxMjM0NTY3ODkwIiwidXNlck5hbWUiOiAiSm9obiBEb2UiLCByb2xlczogWyJhZG1pbiIsInVzZXJzIl19CiAgICAgCiAgCiAgICA=

我正在使用下面的util代码来获得我的解码字符串:

Base64.getDecoder().decode(encoded);
String decripted=new String (decoded);
return decripted

如果数据是bas64编码的json字符串,可以这样做:

objectMapper.readValue(Base64.getDecoder().decode(data),  PtsbUser.class);

但是您的原始字符串无效json:

{userId:"1234567890",userName: "John Doe", roles: ["admin","users"]}  

这是有效的:

{"userId":"1234567890","userName": "John Doe", "roles": ["admin","users"]}

objectMapper.writeValueAsString方法,可用于将任何Java值序列化为字符串

此处不需要objectMapper.writeValueAsString(decoded);因为此方法用于java -> json序列化,并且您已经有json字符串来反序列化

解码后的字符串应该是这样的,以便将其转换回java对象(即PtsbUser(。

"{"userId":"1234567890","userName": "John Doe", "roles": ["admin","users"]}";

但从错误中我可以看到你解码的json字符串是

"{userId:"1234567890",userName: "John Doe", roles: ["admin","users"]}n     n  n    "";

在这里,我不确定你是如何解码json字符串的,一旦你的json字符串如上所示被正确解码,你就可以使用以下代码将其转换为PtsbUser对象,如下所示,

String decoded = "{"userId":"1234567890","userName": "John Doe", "roles": ["admin","users"]}";
ObjectMapper objectMapper = new ObjectMapper();
PtsbUser ptsbUser = objectMapper.readValue(decoded, PtsbUser.class);
System.out.println(ptsbUser);

我也希望你的PtsbUser.java课程会是这样的。

public class PtsbUser {
String userId;
String userName;
List<String> roles;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public List<String> getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
}

这是解码字符串的方法

String inputToDecode = "eyJ1c2VySWQiOiIxMjM0NTY3ODkwIiwidXNlck5hbWUiOiAiSm9obiBEb2UiLCByb2xlczogWyJhZG1pbiIsInVzZXJzIl19CiAgICAgCiAgCiAgICA=";
Decoder decoder = Base64.getDecoder();
String decodedString = new String(decoder.decode(inputToDecode ), "UTF-8");
return decodedString;

您将不需要此行,因为您已经将其作为字符串。

String jsonFormat = objectMapper.writeValueAsString(decoded);

这将在解码后的字符串周围加上双引号。此方法仅在将字符串写入输出/响应时使用。

您可以直接使用readValue方法中的解码字符串

objectMapper.readValue(decoded, PtsbUser.class);

您的JSON也有问题。您可以在https://jsonlint.com/

在这里,您需要用双引号包装密钥,使其成为有效的JSON

{
"userId": "1234567890",
"userName": "John Doe",
"roles": ["admin", "users"] 
}

相关内容

  • 没有找到相关文章

最新更新