无法构造 `class name` 的实例(尽管至少在 Creator 上存在)

Sli*_*ppy 13 java spring json jackson

我有以下类,我将其用作请求有效负载:

public class SampleRequest {

    private String fromDate;
    private String toDate;

    // Getters and setters removed for brevity.
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试将它与以下资源一起使用(只是尝试将其打印到屏幕以查看发生的事情):

@PostMapping("/getBySignatureOne")
public ResponseEntity<?> getRequestInfo(@Valid @RequestBody SampleRequest signatureOneRequest) {

    System.out.println(signatureOneRequest.getToDate);
    System.out.println(signatureOneRequest.getFromDate);
}
Run Code Online (Sandbox Code Playgroud)

这是我发送的 JSON 请求:

{
    "fromDate":"2019-03-09",
    "toDate":"2019-03-10"
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

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.jackson.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]]
Run Code Online (Sandbox Code Playgroud)

我很想知道这里出了什么问题,我怀疑这是构造函数的问题,或者我在某处丢失了一些注释,但老实说我不确定我哪里出错了。

And*_*cus 11

您需要一个带有所有参数的构造函数:

public SampleRequest(String fromDate, String toDate) {

    this.fromDate = fromDate;
    this.toDate = toDate;

}
Run Code Online (Sandbox Code Playgroud)

或使用@AllArgsConstructor@Data来自龙目岛。

  • 啊,这样就解决了我的主要错误 - 但由于某种原因,当我将它们打印到屏幕上时,我似乎看到了字段的空值......知道可能是什么原因造成的吗?如果我们不想破坏这个问题,我也碰巧为此提出了一个新问题! (2认同)

Aay*_*rya 8

就我而言,我缺少无参数构造函数

@Data
@AllArgsConstructor
@NoArgsConstructor
Run Code Online (Sandbox Code Playgroud)

对于那些不使用Lombok的人,请在映射 pojo 中添加 no args 构造函数

public ClassA() {
        super();
        // TODO Auto-generated constructor stub
    }
Run Code Online (Sandbox Code Playgroud)

如果您使用相同的,也不要忘记在主文件中添加Restemplate Bean


dex*_*ini 5

您好,您需要编写自定义反序列化器,因为它无法将字符串(fromDate 和 toDate)解析为 Date

{ "fromDate":"2019-03-09", "toDate":"2019-03-10" }

此链接有一个自定义反序列化器入门教程https://www.baeldung.com/jackson-deserialization

解串器可以这样写。

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);
    }
}}
Run Code Online (Sandbox Code Playgroud)

您可以像这样在类本身注册反序列化器。

@JsonDeserialize(using = ItemDeserializer.class)
public class Item {  ...}
Run Code Online (Sandbox Code Playgroud)

或者您可以像这样手动注册自定义解串器

ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Item.class, new ItemDeserializer());
mapper.registerModule(module);
Run Code Online (Sandbox Code Playgroud)