Java 如何在RESTful WS中隐藏带有条件的字段?

Java 如何在RESTful WS中隐藏带有条件的字段?,java,rest,web-services,get,jax-rs,Java,Rest,Web Services,Get,Jax Rs,我有一个名为Report的类,我需要使用restfulws共享它 一次完整地显示其所有属性 只有一次简化版本 通常我会使用@xmltransive之类的东西来隐藏字段,但这会阻止完整版本的工作 有没有办法在输出之前设置一个条件或某种预过滤字段,这样就不会影响同一类的其他用途 我的报告类如下所示: public class Report { private String reportId; private String title; private String conte

我有一个名为Report的类,我需要使用restfulws共享它

  • 一次完整地显示其所有属性
  • 只有一次简化版本
  • 通常我会使用
    @xmltransive
    之类的东西来隐藏字段,但这会阻止完整版本的工作

    有没有办法在输出之前设置一个条件或某种预过滤字段,这样就不会影响同一类的其他用途

    我的报告类如下所示:

    public class Report {
        private String reportId;
        private String title;
        private String content;
        private Date created;
        private Date modified;
    ...
    }
    
    @GET
    @Path("/{reportId}")
    public Report getReport(@PathParam("reportId") String reportId) {
        return Mock.getReport(reportId);
    }
    
    {
       "reportId": "d83badf3",
       "title": "The tales of lamaru",
       "content": "When once the great Imgur started his journey...",
       "created": 1519672434866,
       "modified": 1519672434866
    }
    
    {
       "reportId": "d83badf3",
       "title": "The tales of lamaru"
    }
    
    完整报告的RESTful共享如下所示:

    public class Report {
        private String reportId;
        private String title;
        private String content;
        private Date created;
        private Date modified;
    ...
    }
    
    @GET
    @Path("/{reportId}")
    public Report getReport(@PathParam("reportId") String reportId) {
        return Mock.getReport(reportId);
    }
    
    {
       "reportId": "d83badf3",
       "title": "The tales of lamaru",
       "content": "When once the great Imgur started his journey...",
       "created": 1519672434866,
       "modified": 1519672434866
    }
    
    {
       "reportId": "d83badf3",
       "title": "The tales of lamaru"
    }
    
    我需要的完整输出如下所示:

    public class Report {
        private String reportId;
        private String title;
        private String content;
        private Date created;
        private Date modified;
    ...
    }
    
    @GET
    @Path("/{reportId}")
    public Report getReport(@PathParam("reportId") String reportId) {
        return Mock.getReport(reportId);
    }
    
    {
       "reportId": "d83badf3",
       "title": "The tales of lamaru",
       "content": "When once the great Imgur started his journey...",
       "created": 1519672434866,
       "modified": 1519672434866
    }
    
    {
       "reportId": "d83badf3",
       "title": "The tales of lamaru"
    }
    
    我需要的简短输出应该如下所示:

    public class Report {
        private String reportId;
        private String title;
        private String content;
        private Date created;
        private Date modified;
    ...
    }
    
    @GET
    @Path("/{reportId}")
    public Report getReport(@PathParam("reportId") String reportId) {
        return Mock.getReport(reportId);
    }
    
    {
       "reportId": "d83badf3",
       "title": "The tales of lamaru",
       "content": "When once the great Imgur started his journey...",
       "created": 1519672434866,
       "modified": 1519672434866
    }
    
    {
       "reportId": "d83badf3",
       "title": "The tales of lamaru"
    }
    

    实现这一点需要什么?

    当您想要从JSON序列化和反序列化过程中排除某些类成员时,Jackson有两种不同的注释可供使用。这两个注释是@JsonIgnore和@JsonIgnoreProperties。 @JsonIgnoreProperties是类级别的注释,它希望要排除的属性将以字符串列表的形式显式指示。 @相反,JsonIgnore是一个成员级或方法级的注释,它希望将要排除的属性逐个标记

    试试这个

    public class Report {
        private String reportId;
        private String title;
        @JsonIgnore
        private String content;
        @JsonIgnore
        private Date created;
        @JsonIgnore
        private Date modified;
    ...
    }
    

    当您想从JSON序列化和反序列化过程中排除某些类成员时,Jackson有两种不同的注释可供使用。这两个注释是@JsonIgnore和@JsonIgnoreProperties。 @JsonIgnoreProperties是类级别的注释,它希望要排除的属性将以字符串列表的形式显式指示。 @相反,JsonIgnore是一个成员级或方法级的注释,它希望将要排除的属性逐个标记

    试试这个

    public class Report {
        private String reportId;
        private String title;
        @JsonIgnore
        private String content;
        @JsonIgnore
        private Date created;
        @JsonIgnore
        private Date modified;
    ...
    }
    

    为什么不使用继承呢

    母公司

    孩子


    当您需要完整报告时,将返回类型设置为
    FullReport
    否则
    report

    为什么不使用继承

    母公司

    孩子


    当您需要完整报告集返回类型为
    FullReport
    时,否则
    report

    是,
    @xmltransive
    在这种情况下与
    @JsonIgnore
    具有相同的效果。虽然我需要(1)一个完整的报告JSON和(2)一个简化的报告JSON。如果我在Report类中添加
    @JsonIgnore
    ,我就达到了(2)的目标,但没有达到(1)的目标。是的,
    @xmltransive
    在这种情况下与
    @JsonIgnore
    具有相同的效果。虽然我需要(1)一个完整的报告JSON和(2)一个简化的报告JSON。如果我在Report类中添加
    @JsonIgnore
    ,我就达到了(2)的目标,但没有达到(1)的目标。听起来像是你在寻找