Skip to content

Actuator endpoints support for Jackson's Serialization Views #24378

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

Closed
zeljko-mirovic opened this issue Dec 7, 2020 · 6 comments
Closed

Actuator endpoints support for Jackson's Serialization Views #24378

zeljko-mirovic opened this issue Dec 7, 2020 · 6 comments
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@zeljko-mirovic
Copy link

I am writing actuator endpoint having two @ReadOperation-s, one without @Selector, one with:

  • @ReadOperation without the selector is returning a collection of DTOs. That DTO contains a potentially long List of objects. To avoid cluttering output with all elements of the list, I would like to return only last element of the list in each DTO.
  • @ReadOperation with the selector is returning one DTO. In that case, I want to return all elements of the inner DTO list.

A convenient way to do this is by using Jackson's Serialization Views. Two views are defined, and @JsonView annotations are added to DTO getters and methods annotated with @ReadOperation. The output is not affected by annotations presence.

Simplified example:

public interface DtoView {

    public interface Last {
    }

    public interface All {
    }
}
public class Dto {

    private List<Element> elements;
    ...

    @JsonView(DtoView.Last.class)
    public Element getLastElement() {
        ...
    }

    @JsonView(DtoView.All.class)
    public List<Element> getElements() {
        ...
    }
}
@Endpoint(id = "example")
public class ExampleEndpoint {

    @ReadOperation
    @JsonView(DtoView.Last.class)
    public Collection<Dto> read() {
        ...
    }

    @ReadOperation
    @JsonView(DtoView.All.class)
    public Dto read(@Selector String selector) {
        ...
    }
}

After converting actuator endpoint to @RestContoler output is affected.

Simplified example:

@RestController
public class ExampleControler {

    @GetMapping(path = "/example", produces = MediaType.APPLICATION_JSON_VALUE)
    @JsonView(DtoView.Last.class)
    public Collection<Dto> read() {
        ...
    }

    @GetMapping(path = "/example/{selector}", produces = MediaType.APPLICATION_JSON_VALUE)
    @JsonView(DtoView.All.class)
    public Dto read(@PathVariable String selector) {
        ...
    }
}

Having in mind that Jackson's Serialization Views are supported in Spring Framework since version 4.2, it will be nice to have similar support for actuator endpoints.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 7, 2020
@philwebb
Copy link
Member

philwebb commented Dec 7, 2020

I'm personally not too keen to tie our actuator infrastructure any further Jackson. We have a long standing issue to support other serialization technologies and I think adding direct @JsonView support would make that harder.

If you're happy only supporting Spring MVC, you could look at the @ControllerEndpoint annotation which should allow you to use more MVC features.

@philwebb
Copy link
Member

philwebb commented Dec 7, 2020

Flagging to see what the rest of the team think about adding @JsonView support.

@philwebb philwebb added the for: team-attention An issue we'd like other members of the team to review label Dec 7, 2020
@bclozel
Copy link
Member

bclozel commented Dec 7, 2020

See #20291 for the actuator-specific JSON mapper instance.
Also, we probably want to deprecate/transform the existing @ControllerEndpoint if we want to achieve #20290.

@wilkinsona
Copy link
Member

I'm personally not too keen to tie our actuator infrastructure any further Jackson

That's my feeling too. An alternative to @ControllerEndpoint that also allows you to continue to use the Actuator endpoint infrastructure, you could use an ObjectMapper in your operation methods to turn the DTO into a Map and apply any views at that time.

@zeljko-mirovic
Copy link
Author

@wilkinsona, thank you for the idea to turn the DTO into a Map and return Map instead.

@philwebb philwebb added status: declined A suggestion or change that we don't feel we should currently apply and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Dec 8, 2020
@philwebb
Copy link
Member

philwebb commented Dec 8, 2020

Since there's a work-around, I'm going to politely decline this one. Thanks anyway for the suggestion.

@philwebb philwebb closed this as completed Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

5 participants