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

Integrate Jackson @JsonView [SPR-7156] #11815

Closed
spring-projects-issues opened this issue Apr 30, 2010 · 14 comments
Closed

Integrate Jackson @JsonView [SPR-7156] #11815

spring-projects-issues opened this issue Apr 30, 2010 · 14 comments
Assignees
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 30, 2010

Brandon Whiteman opened SPR-7156 and commented

Jackson's JSONView annotation allows the developer to control which aspects of a method are serialiazed. With the current implementation, the Jackson view writer must be used but then the content type is not available. It would be better if as part of the RequestBody annotation, a JSONView could be specified.


Affects: 3.1.2

Issue Links:

Referenced from: commits 51fc3b4, be0b69c

25 votes, 24 watchers

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Moved to SPR.

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

I am not completely sure what you mean, but I believe you can use the ResponseBody class to accomplish this.

@spring-projects-issues
Copy link
Collaborator Author

Robert Pond commented

You can use JsonViews to determine what properties of a bean to include in the Json reponse. I think you can already accomplish this with the renderedAttributes property of the MappingJacksonJsonView though.

@spring-projects-issues
Copy link
Collaborator Author

marc schipperheyn commented

@JsonView is intended to allow the developer to select which attributes to include in a json response on a per view basis. When you are using a ResponseBody, you are not manually creating any jsonViews anymore. As such this functionality should be integrated on @ResponseBody level either as a set of attributes or as an extra annotation, so that a developer can determine per responsebody which values he would like to include in the response. Also, it seems a bit inelegant to maintain a list of modelKeys to render at the view level (apart from the fact that it doesn't allow you to just use @ResponseBody). When the underlying model changes with new attributes to render, you would have to update all your views.

@spring-projects-issues
Copy link
Collaborator Author

Filip Spiridonov commented

Here is an example and the spring black magic workaround:
http://stackoverflow.com/questions/7637467/jacksons-jsonview-jsonfilter-and-spring
I agree, it would be nice to have an ability to simply declare the @JsonView(ViewName.class) next to (or inside of) the @ResponseBody.

@spring-projects-issues
Copy link
Collaborator Author

Alex commented

Here you can find an implementation that can be a starting point: https://github.com/martypitt/JsonViewExample
I would find a cleaner solution if the name of the view could be selected as a property of @ResponseBody annotation.
The same property could be used also for similar task for the XML or other marshallers

@spring-projects-issues
Copy link
Collaborator Author

Elnur Abdurrakhimov commented

I want and need this badly. Any neat solutions yet?

@spring-projects-issues
Copy link
Collaborator Author

Gunnar Hillert commented

This is maybe not the perfect Jira - but for XD-999 it would have been nice to "fine-tune" Jackson serialization on a per-controller basis. We had to add a Jackson MixIn, but only needed that for one controller to suppress serialization for one property. The problem is that when doing it on a more global level you may introduce unnecessary project dependencies for other sub-projects.

@spring-projects-issues
Copy link
Collaborator Author

Willie Wheeler commented

+1

I have a RESTful web service that exposes collection resources (e.g., /data-centers) and single resources (e.g., /data-centers/chandler). The collection resource endpoints present a more summary view of the data, whereas the single resource endpoints are more detailed. For the collection resource endpoints, I want to use @JsonView to suppress fields not included in that view, as opposed to having them show up null. (Besides keeping the representation tidy, I'd like to distinguish unused fields from fields that have null values.) The single resource endpoints should show all fields.

So the JSON view varies from endpoint to endpoint, and it would be useful to have a clean way to communicate this to the ObjectMapper.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

We have an initial proposal ready that provides support for Jackson's @JsonView on @ResponseBody controller methods or methods returning ResponseEntity. See this commit here for details including a reference documentation update.

If anyone is interested please try it out with 4.1.0.BUILD-SNAPSHOT via repo.spring.io/snapshot.

Either way comments welcome!

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Marking resolved for now but feel free to comment.

@spring-projects-issues
Copy link
Collaborator Author

Marcel Overdijk commented

Any chance to backport this to 4.0.x?

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Deleuze commented

Current 4.0.x status is that only critical fixes can be integrated in order to avoid regressions, so it will be only available starting 4.1.x.

@spring-projects-issues
Copy link
Collaborator Author

Benjamin Conlan commented

Is there a way to have this play nicely with the @Secured annotation so weaker roles only see a subset of the object?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants