你好stackoverflow专家
我有以下具有货币金额的 InvoiceItem 类
import jakarta.json.bind.annotation.JsonbProperty;
import jakarta.json.bind.annotation.JsonbTypeSerializer;
import jakarta.json.bind.serializer.JsonbSerializer;
import jakarta.json.bind.serializer.SerializationContext;
import jakarta.json.stream.JsonGenerator;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.eclipse.persistence.annotations.UuidGenerator;
import org.javamoney.moneta.FastMoney;
@Entity
@UuidGenerator(name = "INVOICE_ITEM_GEN")
@Table(name = "invoice_items")
public class InvoiceItem {
@Getter
@Setter
@jakarta.persistence.Id
@JsonbProperty("id")
@GeneratedValue(generator = "INVOICE_ITEM_GEN")
private String Id;
@Getter
@Setter
@Column(name = "name")
@JsonbProperty("name")
private String Name;
@Getter
@Setter
@Convert(converter = PersistentFastMoney.class)
@Column(name = "price")
@JsonbProperty("price")
private FastMoney Price;
public InvoiceItem() {
}
public InvoiceItem(String name, String price) {
this.setName(name);
this.setPrice(FastMoney.parse(price));
}
}
我在我的函数中从不同的输入成功生成了这个。
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("/invoices")
public class InvoiceSystem {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Invoice CreateNewInvoice(InvoiceInput input) {
var invoice = new Invoice();
invoice.setCreditor(new InvoiceParty());
for (var item: input.getItems()) {
invoice.Items.add(item.toInvoiceItem());
}
return invoice;
}
}
toInvoiceItem 使用带有两个字符串参数的 InvoiceItem 的构造函数。现在我想在创建类后将类作为 JSON 打印回消费者。
但这失败了
<body><h1>HTTP Status 500 - Internal Server Error</h1>
<hr/>
<p><b>type</b> Exception report</p>
<p><b>message</b>Internal Server Error</p>
<p><b>description</b>The server encountered an internal error that prevented it from fulfilling this request.</p>
<p><b>exception</b>
<pre>javax.servlet.ServletException: javax.json.bind.JsonbException: Unable to serialize property 'Items' from com.openflowlabs.faktura.Invoice</pre>
</p><p><b>root cause</b>
<pre>javax.json.bind.JsonbException: Unable to serialize property 'Items' from com.openflowlabs.faktura.Invoice</pre>
</p><p><b>root cause</b>
<pre>javax.json.bind.JsonbException: Unable to serialize property 'price' from com.openflowlabs.faktura.InvoiceItem</pre>
</p><p><b>root cause</b>
<pre>javax.json.bind.JsonbException: Unable to serialize property 'context' from org.javamoney.moneta.FastMoney</pre>
</p><p><b>root cause</b>
<pre>javax.json.bind.JsonbException: Unable to serialize property 'amountType' from javax.money.MonetaryContext</pre>
</p><p><b>root cause</b>
<pre>javax.json.bind.JsonbException: Unable to serialize property 'annotatedInterfaces' from java.lang.Class</pre>
</p><p><b>root cause</b>
<pre>javax.json.bind.JsonbException: Unable to serialize property 'annotatedOwnerType' from sun.reflect.annotation.AnnotatedTypeFactory.AnnotatedTypeBaseImpl</pre>
</p><p><b>root cause</b>
<pre>javax.json.bind.JsonbException: Error getting value on: javax.money.MonetaryAmount</pre>
</p><p><b>root cause</b>
<pre>java.lang.IllegalAccessException: class org.eclipse.yasson.internal.model.ReflectionPropagation cannot access class sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl (in module java.base) because module java.base does not export sun.reflect.annotation to unnamed module @50c1e8e3</pre>
</p><p><b>note</b> <u>The full stack traces of the exception and its root causes are available in the Payara Server
5.2022.1 #badassfish logs.</u></p>
<hr/>
<h3>Payara Server 5.2022.1 #badassfish</h3></body>
我知道我需要一个自定义序列化程序,但我尝试了一天所有可能的方法来制作一个,但我的应用程序根本不会吃注释
如何为org.javamoney.moneta.FastMoney该类创建一个自定义序列化程序,以便我可以简单地注释所有具有此类的字段,@JsonbTypeSerializer(FastMoneySerializer.class)例如 Invoice Item
@Getter
@Setter
@Convert(converter = PersistentFastMoney.class)
@Column(name = "price")
@JsonbProperty("price")
@JsonbTypeSerializer(FastMoneySerializer.class)
private FastMoney Price;
这有可能吗?是否有一些关于我需要如何制作 Serializer 以便它像这样返回 JSON 的文档?
{
"name": "Testing",
"price": "CHF 20"
}
感谢任何指针和/或解决方案我真的被卡住了,找不到关于如何使这些序列化器工作的教程。如果您有一个具有良好文档的图书馆,那也会有所帮助。