Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for @JsonView Jackson annotation #351

Open
sergiorc opened this issue May 29, 2015 · 12 comments
Open

Support for @JsonView Jackson annotation #351

sergiorc opened this issue May 29, 2015 · 12 comments

Comments

@sergiorc
Copy link

Currently, with the default behaviour, it your controller returns Resource, Resources or PagedResources objects, Jackson Mapper is not able to manage @JSONVIEW properly.

It is posible customize Mapper or MixInAnnotation 's in any way to get this work?

@tinne
Copy link

tinne commented Jun 14, 2015

Does Pull request 359 solve your issue?

@sergiorc
Copy link
Author

Sorry, Tinne, could you provide a slightly more detailed explanation on how to use?

@tinne
Copy link

tinne commented Jun 16, 2015

Sure. Say, you have a detail view for some "configuration item" which is represented by a model bean:

@RequiredArgsConstructor @AllArgsConstructor
public class Configuration extends ResourceSupport {

    @Getter
    private final String guid;

    @Getter
    private final String name;

    @Getter @Setter
    private String description;

    @Getter @Setter
    private boolean active;
}

and you need a collection view with a lot of "Configuration" objects but only minimal data, say, guid and name. Then, annotating these by a custom JsonView interface annotation like this,

@RequiredArgsConstructor @AllArgsConstructor
public class Configuration extends ResourceSupport {

    public interface ConfigurationOverview extends Link.LinkView {}

    @Getter
    @JsonView(ConfigurationOverview.class)
    private final String guid;

    @Getter
    @JsonView(ConfigurationOverview.class)
    private final String name;
...
}

and annotating the overview rest controller like this,

RestController
public class ConfigurationController {

    @RequestMapping(value = "/configuration", method = GET)
    @JsonView(Configuration.ConfigurationOverview.class)
    public HttpEntity<Configuration[]> allConfigurations() { ... }
}

you get an JSON 'view' of the configurations.

@sergiorc
Copy link
Author

Thanks Tinne.
This is the same case I had tested, but using ResourceSupport instead Resource.
I'm applying @JSONVIEW annotations directly to the properties of my domain objects, but it's not working (view result is empty).
Is this patch intended to cover also Resource?
Should it be working?

@tinne
Copy link

tinne commented Jun 17, 2015

In order for Resource and Resources to work, an additional patch was necessary, which extends the coverage of the Link.LinkView interface to the content property of these two container classes. It has now been added to the pull request, please give it a try.

@sergiorc
Copy link
Author

Great work Tinne, It works like a charm.
Only one more thing. Does it make sense to maintain the name Link.LinkView?
May be other options be more intuitive.

@tinne
Copy link

tinne commented Jun 18, 2015

I followed the convention introduced by some introductory blog entry, but am free for any alterations. Maybe lifting the marker interface to be a top-level module is also more spring-alike. What do you think?

@sergiorc
Copy link
Author

Yes, I think the View interface should be out of Link class.

@tinne
Copy link

tinne commented Jun 19, 2015

The marker interface is now a top-level module called ResourcesLinksVisible.

@sergiorc
Copy link
Author

It's much clearer for me now. Of course, it keeps working.
Thank you again Tinne.

@odrotbohm
Copy link
Member

See the discussion on the PR.

@drenda
Copy link

drenda commented Mar 20, 2017

I'm trying to use @JSONVIEW inside my Resourse following your example. Unfortunately I don't find LinkView neither ResourcesLinksVisible. Could you help me with a more up to date example? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants