json无法从String反序列化byte类型的值 [英] json Can not deserialize value of type byte from String
问题描述
在Spring java应用程序中,我接收到带有以下输入的REST json请求,其中'mode'字段在java类中被定义为byte。
In Spring java application, I am receiving REST json request with following input where 'mode' field is defined as byte in the java class.
{
"application": "sadsd",
"date": "20161109",
"mode": "A",
"catalogId": 0,
}
pojo -
public class Test {
String application;
String date;
byte mode;
int catalogId;
...
}
获取以下错误 -
"exception": org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Could not read document: Can not deserialize value of type byte from String \"A\":
not a valid Byte value\n at
[Source: java.io.PushbackInputStream@6386b197; line: xx, column: xx]
(through reference chain: com.abc.myInput[\"mode\"]);
`enter code here`nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException:
Can not deserialize value of type byte from String \"A\":
not a valid Byte value\n at [Source: java.io.PushbackInputStream@6386b197; line: xx, column: xx]
(through reference chain: com.abc.myInput[\"mode\"])",`
我是否必须在这里编写序列化器和反序列化器?
是否有任何可用于字段字段或该字段的get / set方法的注释没有写任何额外的代码?
Do i have to write serializer and deserializer here?
Are there any annotations that i can use on byte field or get/set methods of that field without writing any additional code?
推荐答案
我只是将它们声明为String并从输入字符串中获取第一个字节并转换字节为输出字符串。
感谢输入cricket_007和jnbbender。
i just went ahead with declaring them as String and taking first byte from input string and converting byte to string for the output.
Thanks for the input cricket_007 and jnbbender.
这篇关于json无法从String反序列化byte类型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!