JPA瞬态注释和JSON

这是有关JPA Transient批注的以下问题的后续解答 为什么JPA具有@Transient批注?

我有一个我不想保留的瞬态变量,并用瞬态注释标记。但是,当我想从我的rest控制器生成JSON时,此瞬态变量在输出的JSON中不可用。

POJO PublicationVO直截了当,没有花哨的属性,只有一些具有getter和setter和1个瞬态变量的私有属性(已保留)。

@RequestMapping(value = { "{publicationId}"}, method = RequestMethod.GET, produces = "application/json")

@ResponseBody public PublicationVO getPublicationDetailsJSON(@PathVariable(value = "publicationId") Integer publicationId) {

LOG.info("Entered getPublicationDetailsJSON - publicationId: " + publicationId);

//Call method to get the publicationVO based on publicationId

PublicationVO publicationVO = publicationServices.getPublicationByIdForRestCalls(publicationId);

LOG.info("publicationVO:{}", publicationVO);

LOG.info("Exiting getPublicationDetailsJSON");

return publicationVO;

}

The PublicationVO is as follows

    package com.trinity.domain.dao;

import java.util.Calendar;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.ManyToOne;

import javax.persistence.Table;

import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonInclude;

@Entity

@Table(name = "publication")

public class PublicationVO {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

@Column(name = "id", unique = true, nullable = false)

private Integer id;

@Column(name = "publicationName", unique = false, nullable = false, length = 200)

private String publicationName;

@Column(name = "publicationSource", unique = false, nullable = false, length = 45)

private String publicationSource;

@Column(name = "dateAdded", unique = false, nullable = false)

private Calendar dateAdded;

@Transient

private float percentageProcessed;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getPublicationName() {

return publicationName;

}

public void setPublicationName(String publicationName) {

this.publicationName = publicationName;

}

public String getPublicationSource() {

return publicationSource;

}

public void setPublicationSource(String publicationSource) {

this.publicationSource = publicationSource;

}

public Calendar getDateAdded() {

return dateAdded;

}

public void setDateAdded(Calendar dateAdded) {

this.dateAdded = dateAdded;

}

public float getPercentageProcessed() {

return percentageProcessed;

}

public void setPercentageProcessed(float percentageProcessed) {

this.percentageProcessed = percentageProcessed;

}

@Override

public String toString() {

return "PublicationVO [id=" + id + ", publicationName=" + publicationName + ", publicationSource=" + publicationSource + ", dateAdded=" + dateAdded

+ ", percentageProcessed=" + percentageProcessed + "]";

}

}

当我在日志中看到publicationVO的调试语句时,输出中将包含瞬态变量,但在客户端代码中,json响应中将不包含瞬态变量。

回答:

我只是添加了JsonSerializeJsonDeserialize批注。

@Transient

@JsonSerialize

@JsonDeserialize

private String myField;

以上是 JPA瞬态注释和JSON 的全部内容, 来源链接: utcz.com/qa/399237.html

回到顶部