我目前正在开发一个基于 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;
}
我遇到的问题是,代码一直很好,直到 jackson 介入并转换它。 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 调用的性能或如何提高我的应用程序的 Jackson 性能提供任何建议吗?
最佳答案
jackson 的问题
如果您使用 Jackson API 序列化实体类,那么放置 @JsonIgnore 注释可能足以满足一种情况,但不能满足所有情况。
如果您有一些延迟加载的属性,那么一旦 Jackson 序列化对象,这些属性就会被加载,否则您必须急切地加载它们,这肯定会对性能产生不利影响。
如果你的类之间有关系(一对多;多对多等),那么 jackson 在序列化对象时也会加载所有依赖实体,你也可能会遇到循环依赖问题,即父加载子和子加载父。< br/>
当然,有一种方法可以使用 Jackson API 来克服这个问题,但从长远来看,它会成为一个瓶颈。
解决方案
根据我的说法,最好的方法是为您想要返回的内容创建模型类,并在从 Controller 返回之前加载并返回该类。
这有一些优点和缺点。
缺点:
1. 当然会有额外的类和额外的代码需要维护。
优点:
1. 模型类将是独立的,并且不会对实体类有任何依赖。
2.任何一个类的任何改变都不会影响其他类。
3. 不需要渲染的属性可以避免。
4. 您可以轻松修改实体类,并且在模型类发生更改之前, View 中不会发生更改。
希望这有帮助!!
关于java - Jackson 忽略层次结构嵌套类中的 @JsonIgnore 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32361263/