杰克逊JSON提供者无视@JsonIgnore

时间:2015-09-09 12:55:39

标签: java json rest java-ee jackson

我有一个用于访问某些数据的JAX-RS服务。有其他类的链接,我用@JsonIgnore注释。现在,杰克逊提供商忽略了那些注释。

我的对象:

@Entity
@Table(name = "documents")
@XmlRootElement
public class Document implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "version")
    private String version;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 35)
    @Column(name = "document_number")
    private String documentNumber;
    @Basic(optional = false)
    @NotNull
    @Column(name = "document_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date documentDate;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "subDocument", fetch = FetchType.LAZY)
    private List<SubDocument> subDocumentList;

    public Document() {
    }

    public Document(Integer id) {
        this.id = id;
    }

    public Document(Integer id, String version, String documentNumber, Date documentDate) {
        this.id = id;
        this.version = version;
        this.documentNumber = documentNumber;
        this.documentDate = documentDate;
    }

    @XmlAttribute(required = false)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @XmlAttribute(required = true)
    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @XmlAttribute(required = true)
    public String getDocumentNumber() {
        return documentNumber;
    }

    public void setDocumentNumber(String documentNumber) {
        this.documentNumber = documentNumber;
    }

    @JsonSerialize(using = JsonDateSerializer.class)
    @XmlAttribute(required = true)
    public Date getDocumentDate() {
        return documentDate;
    }

    @JsonDeserialize(using = JsonDateDeserializer.class)
    public void setDocumentDate(Date documentDate) {
        this.documentDate = documentDate;
    }

    @JsonIgnore
    public List<SubDocument> getSubDocumentList() {
        return subDocumentList;
    }

    public void setSubDocumentList(List<SubDocument> subDocumentList) {
        this.subDocumentList = subDocumentList;
    }
}

REST ApplicationConfig

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> resources = new java.util.HashSet<>();
    try {
        Class jacksonProvider = Class.forName("org.codehaus.jackson.jaxrs.JacksonJsonProvider");
        resources.add(jacksonProvider);
    } catch(ClassNotFoundException ex) {
        SLOGGER.fatal("could not find class JacksonJsonProvider", ex);
    }
    addMyOtherResourceClasses(resources);
    return resources;
}

REST接口:

@GET
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
@Path("getDocumentById/{id}")
public Document getDocumentById(@PathParam("id") String id) {
    return sbDocuments.getDocumentById(id);
}

因此,即使我的文档中存在@JsonIgnore注释,列表也会映射到输出。它适用于@XmlTransient注释,但由于JAXB得到了使用,我无法添加该注释。

我使用Codehaus实现,但我也尝试过FasterXML,结果相同。我该怎么做才能解决这个问题?服务正在Glassfish 4.1服务器上运行。

1 个答案:

答案 0 :(得分:0)

将@JsonIgnore置于字段属性之上,而不是放在getter方法上。 即。

       final ResourceHandler swaggerUIResourceHandler = new ResourceHandler();
    swaggerUIResourceHandler.setResourceBase("target/classes/api-docs");
    final ServletContextHandler swaggerUIContext = new ServletContextHandler();
    swaggerUIContext.setContextPath("/api-docs");
    swaggerUIContext.setHandler(swaggerUIResourceHandler);

    FilterHolder cors = swaggerUIContext.addFilter(CrossOriginFilter.class,"/*",EnumSet.of(DispatcherType.REQUEST));
    cors.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
    cors.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
    cors.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,POST,HEAD");
    cors.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "Content-Type, api_key, Authorization");

    ServletHolder def = new ServletHolder("default", DefaultServlet.class);
    def.setInitParameter("resourceBase","./http/");
    def.setInitParameter("dirAllowed","false");
    swaggerUIContext.addServlet(def,"/");

    HandlerList handlers = new HandlerList();

handlers.setHandlers(new Handler[] { swaggerUIContext, new DefaultHandler() });
server.setHandler(handlers);