1

I was looking everywhere how to deserialize part of the JSON into an object and the rest into JsonObject.

for example:

{
  "id" : "123",
  "message" : {"subject" : "test sub" , "body" : "test body"}
}

I want to deserialize this JSON into this class:

public class className {
    private String id;
    private transient JsonObject message;

    // getters and setters
}

The problem is that after the deserialization I get empty object {}inside "message".

Does anyone have any idea about it?

======================================================

EDIT:

A little more info, I am using Spring MVC, the JSON is being sent by POST message into my controller.

The controller function looks like this:

public @ResponseBody String publish(@RequestBody final className input, final HttpServletRequest request,
        final HttpServletResponse response) {
    //input.message = {}
}
5
  • Works fine for me. Please post a minimal reproducible example. Commented Mar 22, 2017 at 18:42
  • This works fine for me. Are you sure you're using the right JsonObject type from Gson? Please provide a minimal reproducible example. Commented Mar 22, 2017 at 18:42
  • I am using com.google.gson.JsonObject. I am getting the input into my REST controller using POST request with the JSON in the body. the controller looks like this: public String sendMessage(@RequestBody final className input, ...); I am stopping with breakpoint right after the entrance to the function and check my input, the ID is there but message is empty Commented Mar 22, 2017 at 19:05
  • 1
    So Spring MVC and potentially Jackson are involved. Please provide those details in your question. Is Jackson on the classpath? Commented Mar 22, 2017 at 19:09
  • Thanks, I have edited my question. Commented Mar 22, 2017 at 19:16

1 Answer 1

3

That is because Spring tries to parse your object with Jackson library while your object holds Gson objects (which can't be parsed with Jackson).

Please refer Configure Gson in Spring.

@Configuration
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter < ? >> converters) {
        GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
        converters.add(gsonHttpMessageConverter);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. I am using Spring Boot so I have added to my appConfig: @Bean public HttpMessageConverters customConverters() from the tutorial and on my Application I added @EnableAutoConfiguration(exclude = { JacksonAutoConfiguration.class }) But now "message" is null. not even '{}'. Do you have any other Idea?
OK! I got it. the problem with the null was because I had transient keyword on the message field. I have removed the transient and now it works! Thank you very much!

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.