gpt4 book ai didi

java - 自定义序列化器仅适用于属性,不适用于类

转载 作者:太空宇宙 更新时间:2023-11-04 15:18:14 25 4
gpt4 key购买 nike

我有两个类新闻和新闻列表

新闻类

@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
}

新闻列表.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());
}
}

jackson 序列化新闻时一切正常,但当我想要序列化新闻列表系统时,使用自定义序列化程序并仅打印 ID。如何仅为属性添加序列化程序。

最佳答案

你的序列化器正是这样做的。 Serialize 方法获取 NewsList 并仅打印出 id。请查看 StdSerializer 的“findConvertingContentSerializer”方法,或使用 JPA 的 @Converter 和 @Convert 注释为您的属性注册转换器。

关于java - 自定义序列化器仅适用于属性,不适用于类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20683086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com