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

Clarify the single class restriction with @JsonView for a response body advice [SPR-12270] #16875

Closed
spring-projects-issues opened this issue Sep 29, 2014 · 4 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Alexey Krylov opened SPR-12270 and commented

Currently only one @JsonView class is used:

JsonViewResponseBodyAdvice.java:

	@Override
	protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
			MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {

		JsonView annotation = returnType.getMethodAnnotation(JsonView.class);
		Assert.isTrue(annotation.value().length != 0, "No view class in JsonView annotation on " + returnType);
		bodyContainer.setSerializationView(annotation.value()[0]);
	}

My code:

@JsonView({Category.Default.class, Product.Default.class})
   @RequestMapping("/catalog.json")
   public Collection<Category> getCatalog(@Named("deviceId") @RequestParam(value = "device_id", required = false) Integer deviceId) {
       return retrieveCatalog(deviceId);
   }

In this example only Category.Default.class will be used.
It will be very nice to have full support for @JsonView.


Affects: 4.1 GA

Referenced from: commits a3fd00e, ae43b17

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Sep 29, 2014

Sébastien Deleuze commented

Hi Alexey,

@JsonView was originally created to annotate a field with one or multiple views, but like JAX-RS @JsonView support (more details at http://wiki.fasterxml.com/JacksonJsonViews), we are reusing this annotation in Spring MVC for another purpose: enabling a view on a per handler method basis, and in this context Jackson API does not support multiple views.

I am going to add a check in order to actively reject multiple view classes in this context, and update our documentation accordingly.

For your need, you should use a single view interface that will inherit of multiple other ones. Jackson will use the view specified and all the inherited ones to serialize the output.

Please also notice that we are going to change the default configuration in the upcoming 4.1.1 release when using @EnableWebMvc or <mvc:annotation-driven />. By default, SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION will be set to false in order to avoid including non annotated fields/properties in the output when @JsonView is used on a method handler. See #16793 for more details.

Regards,
Sébastien Deleuze

@spring-projects-issues
Copy link
Collaborator Author

Alexey Krylov commented

Hi Sebastien,
Will try this approach with one composite interface.
Thanks.

@spring-projects-issues
Copy link
Collaborator Author

Alexey Krylov commented

Yes, it works!
Thanks!

public interface NativeClient extends Category.WithProductsCount, Product.WithSections, Product.WithoutDescription {
}
...

@JsonView(NativeClient.class)

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Sébastien Deleuze, I've revised JsonViewResponseBodyAdvice's assertion to insist on exactly 1 class specified there. Let's mark this issue as resolved once we've also documented that as discussed on the team call today.

Juergen

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 4.1.1 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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