我有两类新闻和新闻列表
News.class
@Entity
@Table(name = News.TABLE_NAME)
@NamedQueries({
@NamedQuery(name = News.FIND_ALL, query = "SELECT n FROM News n"),
@NamedQuery(name = News.FIND_BY_SWID, query = "SELECT n FROM News n WHERE n.swId = :swId"),
@NamedQuery(name = News.FIND_BY_LINK, query = "SELECT n FROM News n WHERE n.link = :link"),
@NamedQuery(name = News.FIND_BY_LIST, query = "SELECT n FROM News n WHERE n.list = :list ORDER BY n.lastModified DESC") })
public class News {
public static final String TABLE_NAME = "news";
public static final String ID_SEQ = "seq_news_id";
public static final String FIND_ALL = "News.findAll";
public static final String FIND_BY_SWID = "News.findBySwId";
public static final String FIND_BY_LINK = "News.findByLink";
public static final String FIND_BY_LIST = "News.findByList";
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ID_SEQ)
@SequenceGenerator(name = ID_SEQ, allocationSize = 1, initialValue = 1, sequenceName = ID_SEQ)
private long id;
@Column(name = "swid", nullable = false, unique = true)
private long swId;
@Column(name = "link", nullable = false, unique = true)
private String link;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "lastmodified")
private Date lastModified;
@Column(name = "title", nullable = false)
private String title;
@Column(name = "lead", nullable = false, length = 500)
private String lead;
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "content", nullable = true)
private String content;
@ElementCollection(fetch = FetchType.LAZY, targetClass = Media.class)
@CollectionTable(name = "news_media")
private List<Media> medias = new ArrayList<Media>();
@ManyToOne(fetch = FetchType.LAZY, targetEntity = NewsList.class, optional = false)
@JoinColumn(name = "newslist_id", nullable = false)
@JsonProperty(value = "newslist_id")
@JsonSerialize(using = NewsListPropertySerializer.class)
private NewsList list;
//Getters and Setters
}
NewsList.class
@Entity
@Table(name = NewsList.TABLE_NAME)
@NamedQueries({
@NamedQuery(name = NewsList.FIND_ALL, query = "SELECT nl FROM NewsList nl"),
@NamedQuery(name = NewsList.FIND_BY_NAME, query = "SELECT nl FROM NewsList nl WHERE nl.name = :name"),
@NamedQuery(name = NewsList.FIND_BY_TYPE, query = "SELECT nl FROM NewsList nl WHERE nl.type = :type"),
@NamedQuery(name = NewsList.FIND_BY_GROUP, query = "SELECT nl FROM NewsList nl WHERE nl.group = :group"),
@NamedQuery(name = NewsList.FIND_NOMAIN_GROUP, query = "SELECT nl FROM NewsList nl WHERE nl.group <> org.ewicom.pps.webservice.news.NewsListGroup.MAIN") })
public class NewsList {
public static final String TABLE_NAME = "newslist";
public static final String ID_SEQ = "seq_newslist_id";
public static final String FIND_ALL = "NewsList.findAll";
public static final String FIND_BY_NAME = "NewsList.findByName";
public static final String FIND_BY_TYPE = "NewsList.findByType";
public static final String FIND_BY_GROUP = "NewsList.findByGroup";
public static final String FIND_NOMAIN_GROUP = "NewsList.finNoMainGroup";
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = ID_SEQ)
@SequenceGenerator(name = ID_SEQ, allocationSize = 1, initialValue = 1, sequenceName = ID_SEQ)
private long id;
@Column(name = "listname", nullable = false, unique = true)
private String name;
@Enumerated(EnumType.STRING)
@Column(name = "listtype", nullable = false)
private NewsListType type;
@Enumerated(EnumType.STRING)
@Column(name = "listgroup", nullable = false)
private NewsListGroup group;
@Column(name = "listlink", nullable = false, unique = true)
private String link;
@OneToMany(targetEntity = News.class, cascade = CascadeType.REMOVE, mappedBy = "list", fetch = FetchType.LAZY, orphanRemoval = true)
@OrderBy("lastModified DESC")
@JsonIgnore
private List<News> news;
//Getters and Setters
}
我在News.class
中为属性列表添加了自定义序列化程序public class NewsListPropertySerializer extends StdSerializer<NewsList> {
public NewsListPropertySerializer(Class<NewsList> t) {
super(t);
}
@Override
public void serialize(NewsList value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonGenerationException {
jgen.writeNumber(value.getId());
}
}
杰克逊序列化新闻时一切正常,但是当我想序列化新闻列表系统时,使用自定义序列化器并仅打印ID。如何仅为属性添加序列化程序。
答案 0 :(得分:0)
您的序列化程序就是这样做的。 serialize方法获取NewsList并打印出id。请查看方法&#39; findConvertingContentSerializer&#39;使用@Converter和JPA的@Convert注释为StdSerializer注册或为您的属性注册转换器。