There is not documentation how to filter fields using JsonViews using specific security roles when using Webflux and non-functional endpoints, for example @RestController
.
There is a tutorial that explains how to achieve this using MCV. https://www.baeldung.com/spring-security-role-filter-json
There is another issue that has been closed that explains similar functionality with RouterFunctions and Webflux. https://github.com/spring-projects/spring-framework/issues/23150
Comment From: rstoyanchev
@monstrfolk you have seen JSON Views section in the docs?
Comment From: monstrfolk
How can the JsonView be set within the controller method or dynamically at runtime?
Would like to set the view dynamically based off of some other property. The 2 examples in original post support dynamically setting the JsonView.
Comment From: rstoyanchev
I see. Have you tried returning MappingJacksonValue
from the controller? That should work.
Comment From: monstrfolk
@rstoyanchev, thanks for you help. Could you please provide an example?
Comment From: rstoyanchev
@RestController
public class FooController {
@GetMapping("/")
MappingJacksonValue handle() {
Foo foo = new Foo();
return new MappingJacksonValue(foo);
}
}
**Comment From: monstrfolk**
How can something as following be achieved?
`
@GetMapping("/")
public Mono<ResponseEntity<?>> get() {
// Return mono response entity with mapping jackson value
}
`
**Comment From: rstoyanchev**
I don't understand what you're missing. You would do exactly as your comment says: "Return mono response entity with mapping jackson value". For example:
```java
@RestController
public class FooController {
@GetMapping("/")
Mono<ResponseEntity<MappingJacksonValue>> handle() {
Mono<Foo> fooMono = ... ;
return fooMono.map(foo -> ResponseEntity.ok(new MappingJacksonValue(foo)));
}
}
Comment From: jnfeinstein
@rstoyanchev is there a setting I need to enable to allow WebFlux w/ annotation routing recognize MappingJacksonValue
? I have confirmed that the @JsonView
annotation works fine. The responsible code is here, and I cannot find the codepaths responsible for encoding a value wrapped by MappingJacksonValue
. The current behavior is that Jackson serializes the MappingJacksonValue
directly, which is not terribly helpful. I am using v5.2.10.
Here is a demo repo exemplifying the problem.
Comment From: rstoyanchev
@jnfeinstein on the WebFlux side the Encoder
contract allows us to pass hints so we don't need to wrap with MappingJacksonValue
but we're missing the ability to do so programmatically. Please, create a new issue and we'll add support for that.
Comment From: jnfeinstein
Thanks @rstoyanchev. I've opened #26035.