如何将一个空列表转换为一个空列表?

huangapple go评论81阅读模式
英文:

how to convert a null list to an empty list?

问题

在我的CRUD应用程序中,一个POST API接收到以下请求体:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BodyRequest {

    private String description;
    private List<Request> ListRequest; 
}

当请求体中没有传递listRequest时,它变为null。是否有办法在listRequest为null时设置一个空值?我想知道是否有可能在列表上添加一个注解,允许像@Value或@Json那样的修改。

我尝试添加以下内容:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BodyRequest {

    private String description;
    @JsonDeserialize(using = OptimizedListDeserializer.class)
    private List<Request> ListRequest; 
}

public class OptimizedListDeserializer extends StdDeserializer<List<Request>> {

    protected OptimizedListDeserializer(StdDeserializer<?> src) {
        super(src);
        // TODO Auto-generated constructor stub
    }

    @Override
    public List<Request> deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException {
           return new ArrayList<>();
    }
}

但是当我尝试调用服务时,我收到以下错误:

{
    "code": 415,
    "error": "Content type 'application/json;charset=UTF-8' not supported"
}

(以上为翻译内容,代码部分未翻译,如有需要,请复制粘贴原文到代码编辑器中查看。)

英文:

in my crud application a post api receives the following body

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BodyRequest {

	private String description;
	private List&lt;Request&gt; ListRequest; 
}

when listRequest is not passed in the body of the request it becomes null
is there any way to set an empty value for the listRequest list when it is null?
I would like to know if there is the possibility to add an annotation on the list that allows such modification as @ value or @json

I tried to add
@JsonDeserialize (using = OptimizedListDeserializer.class) to the list and to define the OptimizedListDeserializer class

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BodyRequest {

    private String description;
    @JsonDeserialize (using = OptimizedListDeserializer.class)
    private List&lt;Request&gt; ListRequest; 
}

public class OptimizedListDeserializer extends StdDeserializer&lt;List&lt;Request&gt; &gt; {


	protected OptimizedListDeserializer(StdDeserializer&lt;?&gt; src) {
		super(src);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List&lt;Request&gt;  deserialize(JsonParser jp, DeserializationContext ctxt)
			throws IOException {
	       return new ArrayList&lt;&gt;();
	}
}

but when i try to call the service i get

&quot;code&quot;: 415,
&quot;error&quot;: &quot;Content type &#39;application/json;charset=UTF-8&#39; not supported&quot;

答案1

得分: 1

以下是翻译好的内容:

最简单的方法是在类中像这样初始化列表:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BodyRequest {

    private String description;
    private List<Request> ListRequest = new ArrayList(); 
}
英文:

the simplest way to do that is initialize the list in the class like this

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BodyRequest {

    private String description;
    private List&lt;Request&gt; ListRequest = new ArrayList(); 
}

答案2

得分: 0

从jackson 2.9版本开始,您可以这样做:
@JsonSetter(nulls = Nulls.AS_EMPTY) public void setStrings(List<String> strings) { this.strings = strings; }

英文:

As of jackson 2.9 you got this :
@JsonSetter(nulls = Nulls.AS_EMPTY)
public void setStrings(List&lt;String&gt; strings) {
this.strings = strings;
}

huangapple
  • 本文由 发表于 2020年4月8日 18:08:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61098156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定