13

I have a third party domain object that I wish to serialize to JSON using Jackson. There are a lot of properties on this accessible via public getters, but I am only interested in a very small subset of these. Since this is a third party object, I went with the mixin route. However, I couldn't find a good way to exclude everything from the original class other than the ones defined on the mixin interface. I tried to specify the @JsonIgnoreProperties on the mixin class, but it quickly gets out of hand with large number of properties to ignore. Any workarounds?

Thanks in advance!

EDIT: Adding some code

public class SpecialObject {
private String name;
private Integer id;
public String getName() {
    return name;
}
public Integer getId() {
    return id;
}
public String getFoo() {
    return "foo";
}
}

public interface SpecialObjectMixin {
    @JsonProperty
    String getName();
}

I was hoping that I will only get name in the serialized JSON. BTW, I am using this for restful calls via cxf-jaxrs with Jackson as the provider.

Kilokahn
  • 2,281
  • 1
  • 25
  • 50

1 Answers1

16

Figured out a way

@JsonAutoDetect(getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
public interface SpecialObjectMixin {

    @JsonProperty
    String getName();
}
Kilokahn
  • 2,281
  • 1
  • 25
  • 50
  • +1 there are some good examples of how to alternatively apply these settings globally on the `ObjectMapper` here: http://stackoverflow.com/q/7105745/1756430. – Sam Berry Jun 01 '15 at 12:39