Select JsonView to Serialize HttpResponse at Runtime

We have a REST API utility with backstasy spring support using Jackson.

I want to decide at runtime (inside my resource class) which JsonView should use to serialize the HttpResponse.

@Path("/foos")
public FooResource {
    @GET
    @Path("/{id}")
    HttpResponse<FooRepresentation> getById(@PathParam("id") @NotNull String id);
}

public class FooRepresentation {
    @JsonView(Views.Short.class)
    private String name;

    private String description;
}

publi class FooResourceImpl {
    public HttpResponse<FooRepresentation> getById(String id) {
        Foo foo = ...;
        // How to decide here to use e.g. the Views.Short.class view?
        return foo;
    }
}

      

I am experimenting with a custom serializer but it seems that the view cannot be customized once the mapper object is configured. What approach might be feasible for this?

+3


source to share





All Articles