2

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?

5
  • 2
    Why can't the model use a String variable? JSON has no concept of a byte array. You could maybe write your setters and getters to use the constructors of the String class to use byte arrays Commented Nov 10, 2016 at 17:52
  • As model is generated and used by many others , altering datatype is not an option. Commented Nov 10, 2016 at 18:48
  • Then you'll need a custom deserializer. Commented Nov 10, 2016 at 18:57
  • I know this is a little long winded but you can use GSON and write a little byte[] adapter. Check out this posting - it pretty much hands it to you. stackoverflow.com/questions/25522309/… Commented Nov 10, 2016 at 20:55
  • what if you have a file object that you're taking as a byte array? Commented Jun 6, 2019 at 6:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

can you plz give example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.