Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring and @JsonView with multiple identifiers

Spring supports @JsonView since version 4.1.

Annotating a Spring contoller's (annotated with @RestController) method with @JsonView that has multiple identifiers I got the following exception:

java.lang.IllegalArgumentException: @JsonView only supported for request body advice with exactly 1 class argument: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@a566e37e

Apparently according to the JsonViewResponseBodyAdvice's Javadoc the following is true:

Note that despite @JsonView allowing for more than one class to be specified, the use for a response body advice is only supported with exactly one class argument. Consider the use of a composite interface.

Likewise when annotating a controller's method argument (annotated with @RequestBody) also with @JsonView that has multiple identifiers; according to the JsonViewRequestBodyAdvice's Javadoc:

Note that despite @JsonView allowing for more than one class to be specified, the use for a request body advice is only supported with exactly one class argument. Consider the use of a composite interface.

Does anybody knows if a fix is planned? My current Spring version is 4.2.4.

This would be extremely useful creating json views for public, private (extending public), summary and detailed (extending summary) views and then combining them in the controller methods!

like image 514
Mike Argyriou Avatar asked Dec 30 '15 19:12

Mike Argyriou


People also ask

What is JsonView annotation?

The JsonView annotation can be used to include/exclude a property during the serialization and deserialization process dynamically. We need to configure an ObjectMapper class to include the type of view used for writing a JSON from Java object using the writerWithView() method.

What is default_ View_ INCLUSION?

DEFAULT_VIEW_INCLUSION. Feature that determines whether properties that have no view annotations are included in JSON serialization views (see JsonView for more details on JSON Views).

What is MappingJacksonValue?

public class MappingJacksonValue extends Object. A simple holder for the POJO to serialize via MappingJackson2HttpMessageConverter along with further serialization instructions to be passed in to the converter.

What are JSON views?

A JSON view is one of the predefined view types that are available in SAPUI5. The JSON view type is defined in a file. The file name has to either end with . view. json or as a JSON string.


1 Answers

As explained in Jackson JsonView documentation, "only single active view per serialization; but due to inheritance of Views, can combine Views via aggregation".

Concretely, if you want to use both Foo and Bar JsonViews, define a FooBar interface that combine them as following:

interface Foo {}

interface Bar {}

interface FooBar extends Foo, Bar {}

You can then annotate your fields with @JsonView(Foo.class) or @JsonView(Bar.class) and use @JsonView(FooBar.class) at controller level.

like image 51
Sébastien Deleuze Avatar answered Oct 06 '22 01:10

Sébastien Deleuze