简体   繁体   中英

Use @JsonView (Jackson) in JAX-RS within Websphere Application Server v8.5

I'm building a Rest service which must be deployed within Websphere Application Server v8.5 (WAS8.5). WAS8.5 uses Jackson 1.6.2 as JSON mapper. The following annotations are supported:

org.codehaus.jackson.annotate.JsonAnySetter
org.codehaus.jackson.annotate.JsonAutoDetect
org.codehaus.jackson.annotate.JsonClass
org.codehaus.jackson.annotate.JsonContentClass
org.codehaus.jackson.annotate.JsonCreator
org.codehaus.jackson.annotate.JsonGetter
org.codehaus.jackson.annotate.JsonIgnore
org.codehaus.jackson.annotate.JsonIgnoreProperties
org.codehaus.jackson.annotate.JsonKeyClass
org.codehaus.jackson.annotate.JsonProperty
org.codehaus.jackson.annotate.JsonPropertyOrder
org.codehaus.jackson.annotate.JsonSetter
org.codehaus.jackson.annotate.JsonSubTypes
org.codehaus.jackson.annotate.JsonSubTypes.Type
org.codehaus.jackson.annotate.JsonTypeInfo
org.codehaus.jackson.annotate.JsonTypeName
org.codehaus.jackson.annotate.JsonValue
org.codehaus.jackson.annotate.JsonWriteNullProperties
org.codehaus.jackson.map.annotate.JsonCachable
org.codehaus.jackson.map.annotate.JsonDeserialize
org.codehaus.jackson.map.annotate.JsonSerialize
org.codehaus.jackson.map.annotate.JsonTypeIdResolver
org.codehaus.jackson.map.annotate.JsonTypeResolver
org.codehaus.jackson.map.annotate.JsonView

In my code I want to use the JsonView annotation. The next code example explains what I'm trying to accomplish.

POJO:

public class SomeObject {
    @JsonView(SomeObjectView.Details.class)
    private String somePropOne;

    private String somePropTwo;
    ...
}

View interface:

public interface SomeObjectView {
    public interface Details{}
}

Rest controller:

@Stateless
@Path("/someobjects")
public class SomeObjectRestController {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("{id}")
    @JsonView(SomeObjectView.Details.class)
    public SomeObject getPolis(@PathParam("id") String id) {
        SomeObject someObj = ... // Service call
        return someObj;
    }
}

In the above example the @JsonView in the Rest Controller seems to have no effect (both properties show up in the JSON response). Probably the JAX-RS implementation of Websphere does nothing with the annotation on the Rest Endpoint.

Is there any way to get this to work?

Second property serialised because it is "View-less" property:

"By default all properties without explicit view definition are included in serialization. "

You can change this by:

objectMapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false);

Do not forget to annotate your unwanted properties (I use static classes):

class SomeObjectView {
    static class Details{}
    static class UnWantedDetails{}

}
public class SomeObject {
    @JsonView(SomeObjectView.Details.class)
    private String somePropOne;

    @JsonView(SomeObjectView.UnWantedDetails.class)
    private String somePropTwo;
    ...
}

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM