英文:
Deserialization of JSON with integer keys and string value in Spring-boot
问题
以下是翻译的内容:
我必须在Spring Boot中使用Feign库对具有整数键和字符串值的简单JSON进行反序列化和序列化。我知道,如果键是字符串,那么在Spring Boot中使用自动序列化和反序列化是非常直接的。我的JSON如下:
"avatar": {
"48x48": "https://wfrjira.int.abc.com/secure/useravatar?ownerId=person&avatarId=1234",
"24x24": "https://wfrjira.int.abc.com/secure/useravatar?size=small&ownerId=person&avatarId=1234"
}
我的POJO Bean类具有适当的getter和setter方法:
class Avatar {
private String _48x48;
private String _24x24;
public Avatar() {
}
public Avatar(String _48x48, String _24x24) {
this._48x48 = _48x48;
this._24x24 = _24x24;
}
// Getter和Setter方法...
}
我得到了以下错误:
> 发生了意外错误(类型=内部服务器错误,状态=500)。
> 在提取响应类型 [class [Lcom.XXXXXX.JiraResourceData;] 和内容类型 [application/json;charset=UTF-8] 时出错;嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从 START_OBJECT 令牌反序列化实例 [Lcom.XXXXXX.JiraResourceData;];嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌反序列化实例 [Lcom.XXXXXX.JiraResourceData;],位置:[Source: (PushbackInputStream); 行:1,列:1]
feign.codec.DecodeException: 在提取响应类型 [class [Lcom.XXXXXX.JiraResourceData;] 和内容类型 [application/json;charset=UTF-8] 时出错;嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从 START_OBJECT 令牌反序列化实例 [Lcom.XXXXXX.JiraResourceData;];嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT 令牌反序列化实例 [Lcom.XXXXXX.JiraResourceData;],位置:[Source: (PushbackInputStream); 行:1,列:1]
我理解我需要在Avatar类中使用与JSON键匹配的名称声明变量,但字符串不能以数字开头。在Spring Boot中是否有其他解决方法?任何帮助将不胜感激。
英文:
I have to de-serialize and serialize back a simple JSON having integer key and string value in Spring-boot using Feign library. I know, if the key is in String, this is pretty straight forward in Spring-boot with automatic serialization and de-serialization. My JSON looks like :
"avatar": {
"48x48": "https://wfrjira.int.abc.com/secure/useravatar?ownerId=person&avatarId=1234",
"24x24": "https://wfrjira.int.abc.com/secure/useravatar?size=small&ownerId=person&avatarId=1234"
}
My POJO bean with proper getters and setters class is :
class Avatar {
private String _48x48;
private String _24x24;
public Avatar() {
}
public Avatar(String _48x48, String _24x24) {
this._48x48 = _48x48;
this._24x24 = _24x24;
} Getters and Setter ...
I am getting following error :
> There was an unexpected error (type=Internal Server Error, status=500).
Error while extracting response for type [class [Lcom.XXXXXX.JiraResourceData;] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData;
out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData;
out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 1]
feign.codec.DecodeException: Error while extracting response for type [class [Lcom.XXXXXX.JiraResourceData;] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData;
out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of [Lcom.XXXXXX.JiraResourceData;
out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 1]
I understand that I need to declare the variables in Avatar class with the name matching with the keys of JSON but strings can't be declared starting with numeric. Is there any other way to resolve this in Spring-boot? Any help would be appreciated.
答案1
得分: 2
首先,您最后的JSON
行有一个逗号。请移除该逗号。此外,应将该JSON
嵌套在另一组括号内。
另外,为您的类添加注解@JsonRootName("avatar")
,因为您的类名是大写的,但您的JSON
是小写的。
此外,您可能希望将字段重命名为JSON
中的字段名,或者使用注解@JsonProperty("my-json-name")
进行标注。
英文:
First of all, your last JSON
line has a comma. Remove that. Furthermore, the JSON
should be embedded in another set of brackets.
Also, annotate your class with @JsonRootName("avatar")
since your class is uppercase but your JSON
is lowercase.
Furthermore, you probably want to rename your fields to the field names in the JSON
or annotate these with @JsonProperty("my-json-name")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论