spring-projects / spring-projects/spring-hateoas

Resource JSON serialization produce empty "content" property

Open
#277 1 comment 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since May 21, 2015.

process: waiting for feedback
Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

Hi,

here is my controller code:

@RequestMapping(value= "/test-resource", method = RequestMethod.GET)
public TestResource test() {
    return new TestResource(new Test());
}

private class TestResource extends Resource<Test> {
    public TestResource(Test content, Link... links) {
        super(content, links);
    }
}

private class Test {}

And I use the MappingJackson2HttpMessageConverter as an ObjectMapper.

The result of requesting my controller's method is:

{
    "content": {

    }
}

But in case of Test class is an @Entity or a @Component, it serialized correctly:

{ }

The reason of such behaviour is that Jackson uses UnwrappingBeanSerializer to serialize @Entity and unwrap "content" property, but in case of POJO Jackson uses UnknownSerializer that serialize content as is.

The problem can be solved by setting @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY) class annotation on TestMixIn, but it looks like a hack.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.