在Jax-rs中,如何在json输出中将空值呈现为空字符串

时间:2022-08-18 11:51:16

I currently have a JPA entity like this

我目前有一个像这样的JPA实体

@XmlNullPolicy(emptyNodeRepresentsNull = true, nullRepresentationForXml = XmlMarshalNullRepresentation.EMPTY_NODE )
@XmlRootElement
@Entity
public class Entity implements Serializable {
   @id
   @Column
   private Long id;
   @Column
   private String field2;
   @Column 
   private BigDecimal field3;
   @Column
   private BigDecimal field4;

When I return the entity in a jax-rs web service I've got :

当我在jax-rs web服务中返回实体时,我得到了:

{ "id" : 12345 , "field2" : "Hello world" , "field3" : null, "field4" : null }

I would like to have an output like

我希望有一个输出

{ "id" : 12345 , "field2" : "Hello world" , "field3" : "", "field4" : "" }

What is the best idiom to achieve this ?

实现这一目标的最佳成语是什么?

I am using weblogic 12c and my JAXB implementation is eclipselink-moxy.

我使用的是weblogic 12c,我的JAXB实现是eclipselink-moxy。

1 个解决方案

#1


1  

I personally think that it is not a good practice. You are loosing information when using an empty String. null means that it is not set and the empty String object means that it is set with empty value. I think that the client should handle this, but if you want to loose that kind of information You can create custom serialisation like the one mentioned here.

我个人认为这不是一个好习惯。使用空String时丢失信息。 null表示未设置它,空String对象表示它设置为空值。我认为客户端应该处理这个问题,但是如果你想放弃那种信息你可以像这里提到的那样创建自定义序列化。

#1


1  

I personally think that it is not a good practice. You are loosing information when using an empty String. null means that it is not set and the empty String object means that it is set with empty value. I think that the client should handle this, but if you want to loose that kind of information You can create custom serialisation like the one mentioned here.

我个人认为这不是一个好习惯。使用空String时丢失信息。 null表示未设置它,空String对象表示它设置为空值。我认为客户端应该处理这个问题,但是如果你想放弃那种信息你可以像这里提到的那样创建自定义序列化。