chappalprasad chappalprasad - 14 days ago 5
Java Question

json Can not deserialize value of type byte from String

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;
...
}


getting Following error -

"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\"])",`


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?

Answer

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.

Comments