Java 无法构造 `class name` 的实例(尽管至少在 Creator 上存在)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55032293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot construct instance of `class name` (although at least on Creator exists)
提问by Slippy
I have the following class, which I am using as a request payload :
我有以下类,我将其用作请求有效负载:
public class SampleRequest {
private String fromDate;
private String toDate;
// Getters and setters removed for brevity.
}
I am trying to use it with this resource below (just trying to print it to screen to see things happen) :
我正在尝试将它与下面的这个资源一起使用(只是试图将它打印到屏幕上以查看发生的事情):
@PostMapping("/getBySignatureOne")
public ResponseEntity<?> getRequestInfo(@Valid @RequestBody SampleRequest signatureOneRequest) {
System.out.println(signatureOneRequest.getToDate);
System.out.println(signatureOneRequest.getFromDate);
}
This is the JSON request I send up :
这是我发送的 JSON 请求:
{
"fromDate":"2019-03-09",
"toDate":"2019-03-10"
}
This is the error I get :
这是我得到的错误:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.test.app.payload.SampleRequest` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('fromDate'); nested exception is com.fasterxml.Hymanson.databind.exc.MismatchedInputException: Cannot construct instance of `com.test.app.payload.SampleRequest` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('fromDate')
at [Source: (PushbackInputStream); line: 1, column: 2]]
I'd love to know what's wrong here, I suspect its an issue with constructors, or that I am missing some annotation somewhere, but I am honestly unsure of where I have gone wrong.
我很想知道这里出了什么问题,我怀疑这是构造函数的问题,或者我在某处丢失了一些注释,但老实说我不确定我哪里出错了。
采纳答案by Andronicus
You need a constructor with all parameters:
您需要一个带有所有参数的构造函数:
public SampleRequest(String fromDate, String toDate) {
this.fromDate = fromDate;
this.toDate = toDate;
}
Or using @AllArgsConstructor
or @Data
from lombok.
或使用@AllArgsConstructor
或@Data
来自龙目岛。
回答by dextermini
Hi you need to write custom deserializer as it not able to parse String (fromDate and toDate) to Date
嗨,您需要编写自定义反序列化器,因为它无法将字符串(fromDate 和 toDate)解析为 Date
{ "fromDate":"2019-03-09", "toDate":"2019-03-10" }
{ "fromDate":"2019-03-09", "toDate":"2019-03-10" }
this link has a tutorial to get started with custom deserializer https://www.baeldung.com/Hymanson-deserialization
此链接有一个教程,可以开始使用自定义反序列化器https://www.baeldung.com/Hymanson-deserialization
Deserializer could be written like this.
解串器可以这样写。
public class CustomDateDeserializer extends StdDeserializer<Date> {
private static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
public CustomDateDeserializer() {
this(null);
}
public CustomDateDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Date deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException {
String date = jsonparser.getText();
try {
return formatter.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}}
You can register the deserializer at Class itself like this.
您可以像这样在 Class 本身注册反序列化器。
@JsonDeserialize(using = ItemDeserializer.class)
public class Item { ...}
Or either you can register custom deserializer manually like this
或者您可以像这样手动注册自定义解串器
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Item.class, new ItemDeserializer());
mapper.registerModule(module);