英文:
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<Request> 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<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<>();
}
}
but when i try to call the service i get
"code": 415,
"error": "Content type 'application/json;charset=UTF-8' not supported"
答案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<Request> 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<String> strings) {
this.strings = strings;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论