找不到类型[simple type,class java.time.LocalDateTime]的合适构造函数

时间:2016-06-14 02:06:36

标签: spring spring-boot redis

我的程序是用Java 8编写的,当我使用LocalDateTime的类型时,它会给我以下错误:

  

找不到类型[simple type,class java.time.LocalDateTime]的合适构造函数:无法从JSON对象实例化(缺少默认构造函数或创建者,或者可能需要添加/启用类型信息?)
  在[来源:[B @ 5976fe6f; line:1,column:80](通过引用链:com.boot.framwork.entity.UserInfo [“accessTime”])

实体是这样的:

public class UserInfo implements Serializable {

private static final long serialVersionUID = 1L;

private String username;

private int age;

private LocalDateTime accessTime;

private Date time;

private List<String> list = new ArrayList<String>();

private boolean isMarried;

public UserInfo() {

}

当我没有使用LocalDateTime

时,它有效

2 个答案:

答案 0 :(得分:1)

在我看来,您没有JSR310支持。 Jackson需要一些额外的配置才能识别Java 8 Date&amp; Time API数据类型。您可以通过项目依赖项添加它,只需在this

上添加依赖项
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.7.4</version>
</dependency>

或者如果您使用Gradle

compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.7.4'

由于你使用SpringBoot,这应该足够了,因此不能手动创建JSR310Module类型的Bean。

答案 1 :(得分:0)

我遇到了同样的问题,需要正确地将import pandas as pd import numpy as np selection = ['cat', 'dog'] df = pd.DataFrame({'molecule': ['a','b','c','d','e'], 'species' : [['dog'], ['horse','pig'],['cat', 'dog'], ['cat','horse','pig'], ['chicken','pig']]}) df1 = df[df['species'].apply((lambda x: 'dog' in x) )] df2=df[df['species'].apply((lambda x: 'cat' in x) )] frames = [df1, df2] result = pd.concat(frames,join='inner',ignore_index=False) print("result",result) result = result[~result.index.duplicated(keep='first')] print(result) 取出和取出MyCustomClass,以便它可以通过Lambda Function中的State Machine传递而没有任何打h。

我使用以下帖子为我提供了最佳答案:

AWS Lambda json deserialization with jackson annotations

这是我的代码对我有用:

Step Function

即使JSON字符串与import java.io.InputStream; import java.io.OutputStream; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.LambdaLogger; import com.amazonaws.services.lambda.runtime.RequestStreamHandler; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; public class StaticSearchPagingLambdaFunctionHandler implements RequestStreamHandler { LambdaLogger logger = null; MyCustomClass myCustomClass = null; // Register the JavaTimeModule for LocalDate conversion ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); @Override public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) { myCustomClass = objectMapper.readValue(inputStream, MyCustomClass .class); // ... // Do stuff with myCustomClass // ... objectMapper.writeValue(outputStream, myCustomClass); } } 写入ObjectMapper的打印方式不同,当下一个lambda函数在通过OutPutStream时将其接收时,它仍然会正确转换为Step Function

确保在LocalDate中,您的MyCustomClass方法可以正确打印。我的toString()方法如下:

toString()

然后,当您的JSON打印输出发送到lambda而不是其他疯狂的import java.time.LocalDate; import org.json.JSONObject; public class SimpleSearch { private LocalDate startDate; private LocalDate endDate; // ... // Getters and Setters for the LocalDate variables // ... @Override public String toString() { return new JSONObject(this).toString(); } public SimpleSearch() {} } 格式时,它将始终如下所示:

Jackson

我使用的一些Maven依赖项:

{
    "startDate": "2018-11-01",
    "endDate": "2018-11-16"
}

希望AWS可以将<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.9.7</version> </dependency> 转换与JSON相互转换,从而使我们不必再使用这些自定义转换。