Jacskon在嵌套的层次结构类中忽略@JsonIgnore注释

时间:2015-09-02 19:10:04

标签: java json spring jackson

我有一个基于Spring的java应用程序,我目前正在开发 长话短说 - 这是我用来从数据库中检索对象的代码,对它进行一些计算并渲染它

@RequestMapping(value = { "/mapping" }, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Object getMasterAppMappingById(
        @PathVariable(value = "masterAppId") Integer masterAppId) {  

    RestBaseVO masterAppMappingRestBaseVO = new RestBaseVO();

    MasterAppMappingTreeDetailsVO masterAppMappingTreeDetailsById = applicationDeviceServices.getMasterAppMappingTreeDetails(masterAppId, true);

    return masterAppMappingTreeDetailsById;
}

我遇到的问题是,代码很好,直到杰克逊开始并转换它。 在return语句是我的瓶颈

方法getMasterAppMappingTreeDetails工作正常并且运行良好

jackson呈现的json显示在pastebin上的以下url中 http://pastebin.com/erRDtweZ

正如你所看到的 - 它相当大

被序列化的类如下

public class MasterAppMappingTreeDetailsVO {

@JsonProperty("id")
private Integer id;

@JsonProperty("mappingId")
private Integer mappingId;

@JsonProperty("parentMappingId")
private Integer parentMappingId;



 @JsonProperty("isQuestion")
    private boolean isQuestion;

    @JsonProperty("isAnswer")
    private boolean isAnswer;

    @JsonProperty("isApplication")
    private boolean isApplication;

    @JsonProperty("displayLabel")
    private String displayLabel;

    @JsonProperty("additionalText1")
    private String additionalText1;

    @JsonProperty("imageUrl")
    private String imageUrl;

    @JsonProperty("imageDateUpdated")
    private Long imageDateUpdated;

    @JsonProperty("appId")
    private Integer appId;

    @JsonProperty("appName")
    private String appName;

    @JsonProperty("children")
    private List<MasterAppMappingTreeDetailsVO> children;

    @JsonProperty("menuStyle")
    private MenuStyleVO menuStyle;

}


    @Entity
@Table(name = "menu_style")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class MenuStyleVO extends BaseDAOVO implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 3697798179195096156L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Integer id;

    @Column(name = "menuStyleName", unique = false, nullable = false, length = 200)
    private String menuStyleName;

    @Column(name = "menuTemplate", unique = false, nullable = false, length = 200)
    private String menuTemplate;

    @OneToOne(fetch = FetchType.EAGER)
    @Cascade({ CascadeType.SAVE_UPDATE })
    @JoinColumn(name="logo_id")
    @JsonProperty("logo")
    private ApplicationImageVO logo;

    @Column(name = "logoAlignment", unique = false, nullable = true, length = 20)
    private String logoAlignment;

    @Column(name = "backArrowColor", unique = false, nullable = true, length = 7)
    private String backArrowColor;    

    @OneToOne(fetch = FetchType.EAGER)
    @Cascade({ CascadeType.SAVE_UPDATE })
    @JoinColumn(name="backArrowIcon_id")
    @JsonProperty("backArrowIcon")
    private ApplicationImageVO backArrowIcon;

    @Column(name = "questionLabelTextColor", unique = false, nullable = true, length = 7)
    private String questionLabelTextColor;  

    @Column(name = "headerBackgroundColor", unique = false, nullable = true, length = 7)
    private String headerBackgroundColor;      

    @Column(name = "headerBackgroundOpacity", unique = false, nullable = true)
    private Integer headerBackgroundOpacity; 

    @Column(name = "mainBackgroundColor", unique = false, nullable = true, length = 7)
    private String mainBackgroundColor;      

    @Column(name = "mainBackgroundOpacity", unique = false, nullable = true)
    private Integer mainBackgroundOpacity; 

    @OneToOne(fetch = FetchType.EAGER)
    @Cascade({ CascadeType.SAVE_UPDATE })
    @JoinColumn(name="backgroundImageLandscape_id")
    @JsonProperty("backgroundImageLandscape")
    private ApplicationImageVO backgroundImageLandscape;

    @OneToOne(fetch = FetchType.EAGER)
    @Cascade({ CascadeType.SAVE_UPDATE })
    @JoinColumn(name="backgroundImagePortrait_id")
    @JsonProperty("backgroundImagePortrait")
    private ApplicationImageVO backgroundImagePortrait;

    @Column(name = "containerColor", unique = false, nullable = true, length = 7)
    private String containerColor;      

    @Column(name = "containerOpacity", unique = false, nullable = true)
    private Integer containerOpacity; 

    @Column(name = "containerLineDividerColor", unique = false, nullable = true, length = 7)
    private String containerLineDividerColor;      

    @Column(name = "containerLineDividerOpacity", unique = false, nullable = true)
    private Integer containerLineDividerOpacity; 

    @Column(name = "optionIconSize", unique = false, nullable = true)
    private Integer optionIconSize; 

    @Column(name = "optionLabelTextColor", unique = false, nullable = true, length = 7)
    private String optionLabelTextColor;     

    @Column(name = "optionTaglinePosition", unique = false, nullable = true, length = 20)
    private String optionTaglinePosition;   

    @Column(name = "optionTaglineTextColor", unique = false, nullable = true, length = 7)
    private String optionTaglineTextColor; 

    @Column(name = "optionSelectionArrowColor", unique = false, nullable = true, length = 7)
    private String optionSelectionArrowColor;

    @OneToOne(fetch = FetchType.EAGER)
    @Cascade({ CascadeType.SAVE_UPDATE })
    @JoinColumn(name="optionSelectionArrowIcon_id")
    @JsonProperty("optionSelectionArrowIcon")
    private ApplicationImageVO optionSelectionArrowIcon;

}

任何人都可以提供有关如何提高此json调用性能或如何提高我的应用程序的杰克逊性能的建议吗?

1 个答案:

答案 0 :(得分:3)

杰克逊的问题
如果您使用Jackson API序列化您的实体类,那么输入@JsonIgnore注释可能只需要一个案例但不是全部。
如果你有一些延迟加载的属性,那么一旦杰克逊序列化对象或者你必须急切加载它们就会加载它们,这肯定会对性能产生负面影响。
如果你的类之间有关系(一个多数;许多个等),那么Jackson在序列化对象时也会加载所有依赖实体,你也可能会遇到一个循环依赖问题,即父加载子和父加载父。登记/> 当然,有一种方法可以使用Jackson API克服这种情况,但从长远来看,它将成为瓶颈。

<强>解决方案
根据我的最佳方法是为要返回的内容创建模型类,并在从控制器返回之前加载并返回该类。

这有一些优点和缺点。

<强>缺点:
1.当然会有额外的类和额外的代码需要维护。

<强>优点:
1.模型类是独立的,不会对您的实体类有任何依赖 2.任何课程的任何变更都不会影响其他课程 3.可以避免不需要渲染的属性 4.您可以轻松修改实体类,直到更改模型类后,视图中不会发生更改。

希望这会有所帮助!!