雅加达EE
@Entity(name = "Book")
@Table(name = "book")
public class Book {
@Id
@GeneratedValue
private Long id;
@NaturalId
private String isbn;
private String title;
@Column(name = "published_on", columnDefinition = "date")
@Convert(converter = YearMonthDateAttributeConverter.class)
private YearMonth publishedOn;
// Getters and setters omitted for brevity
}
我只想插入年份和月份,我使用的是JakartaEE Jsonb。Localdate工作正常,但我也必须插入日期。我不习惯使用Jackson。
public class Book {
public String isbn;
public String title;
public YearMonth publishedOn;
}
public static void main(String[] args) {
// Create a book instance
Book book = new Book();
book.isbn = "123";
book.title = "Advanced JPA and Hibernate";
book.publishedOn = YearMonth.of(2022, 9);
// Create Jsonb and serialize
Jsonb jsonb = JsonbBuilder.create();
var result = jsonb.toJson(book);
}
1条答案
按热度按时间owfi6suc1#
我通过创建
YearMonthTypeAdapter
来实现JsonAdapter<YearMonth, String>
类,然后覆盖下面的方法,找到了一个解决方案:1.