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

Document how to specify Jackson JSON view serialization hints with @RestController and Webflux #25596

Closed
monstrfolk opened this issue Aug 16, 2020 · 10 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Milestone

Comments

@monstrfolk
Copy link

monstrfolk commented Aug 16, 2020

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.
#23150

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 16, 2020
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Aug 20, 2020

@monstrfolk you have seen JSON Views section in the docs?

@rstoyanchev rstoyanchev added status: waiting-for-feedback We need additional information before we can continue in: web Issues in web modules (web, webmvc, webflux, websocket) type: documentation A documentation task labels Aug 20, 2020
@monstrfolk
Copy link
Author

monstrfolk commented Aug 20, 2020

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.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Aug 20, 2020
@rstoyanchev
Copy link
Contributor

I see. Have you tried returning MappingJacksonValue from the controller? That should work.

@rstoyanchev rstoyanchev self-assigned this Aug 21, 2020
@rstoyanchev rstoyanchev removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 21, 2020
@rstoyanchev rstoyanchev added this to the 5.2.9 milestone Aug 21, 2020
@monstrfolk
Copy link
Author

@rstoyanchev, thanks for you help. Could you please provide an example?

@rstoyanchev
Copy link
Contributor

@RestController
public class FooController {

	@GetMapping("/")
	MappingJacksonValue handle() {
		Foo foo = new Foo();
		return new MappingJacksonValue(foo);
	}
}

@monstrfolk
Copy link
Author

monstrfolk commented Sep 2, 2020

How can something as following be achieved?
@GetMapping("/") public Mono<ResponseEntity<?>> get() { // Return mono response entity with mapping jackson value }

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Sep 3, 2020

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:

@RestController
public class FooController {

	@GetMapping("/")
	Mono<ResponseEntity<MappingJacksonValue>> handle() {
		Mono<Foo> fooMono = ... ;
		return fooMono.map(foo -> ResponseEntity.ok(new MappingJacksonValue(foo)));
	}
}

@jnfeinstein
Copy link

jnfeinstein commented Nov 4, 2020

@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.

@rstoyanchev
Copy link
Contributor

@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.

@jnfeinstein
Copy link

Thanks @rstoyanchev. I've opened #26035.

zx20110729 pushed a commit to zx20110729/spring-framework that referenced this issue Feb 18, 2022
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) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

4 participants