在Spring Rest API中配置LocaldateTime

时间:2018-11-18 22:04:48

标签: spring spring-boot spring-data-jpa spring-data

我将Java 10与最新的Spring spring-boot-starter-parent 2.1.0.RELEASE一起使用

POM配置:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>

    <dependencies>          
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.9.7</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.7</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.9.7</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.9.7</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>woodstox-core-asl</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-poi</artifactId>
            <version>1.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth.boot</groupId>
            <artifactId>spring-security-oauth2-autoconfigure</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
    </dependencies>

其他端点:

    @GetMapping("{id}")
    public ResponseEntity<?> get(@PathVariable String id) {
        return transactionRepository
                .findById(Integer.parseInt(id))
                .map(mapper::toDTO)
                .map(ResponseEntity::ok)
                .orElseGet(() -> notFound().build());
    }

DTO:

public class PaymentTransactionsDTO {

    private Integer id;

    private String status;

    private LocalDateTime created_at;

    private String merchant;
    .... getters and setters
}

但是当我尝试为LocalDateTime created_at返回JSON数据时,得到的结果为空。我认为LocalDateTime没有正确转换为JSON值。

您能建议我如何解决此问题吗?

4 个答案:

答案 0 :(得分:3)

JSON序列化由Jackson的ObjectMapper驱动,我建议对其进行显式配置。为了正确序列化Java 8日期和时间对象,请确保

  • 注册JavaTimeModule
  • 禁用书写日期作为时间戳
  • 设置日期格式(使用StdDateFormat

StdDateFormat的描述:

  

标准Date使用的默认DateFormat实现   序列化器和反序列化器。对于序列化,默认情况下使用    ISO-8601 兼容格式(格式字符串“ yyyy-MM-dd'T'HH:mm:ss.SSSZ”)   对于反序列化, ISO-8601和RFC-1123

推荐的配置:

@Configuration
public class JacksonConfig {

    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper()
            .setAnnotationIntrospector(new JacksonAnnotationIntrospector())
            .registerModule(new JavaTimeModule())
            .setDateFormat(new StdDateFormat())
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    }

}     }

序列化日期和时间对象的示例:

  • LocalDate 2018-11-21
  • LocalTime 11:13:13.274
  • LocalDateTime 2018-11-21T11:13:13.274
  • ZonedDateTime 2018-11-21T11:13:13.274 + 01:00

编辑:独立的依赖项(已经在spring-boot-starter-web中传递):

  • com.fasterxml.jackson.core:jackson-annotations
  • com.fasterxml.jackson.core:jackson-databind
  • com.fasterxml.jackson.datatype:jackson-datatype-jsr310

答案 1 :(得分:1)

尝试在created_at字段上使用@JsonFormat。

@JsonFormat(pattern="yyyy-MM-dd")
@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
private LocalDateTime created_at;

答案 2 :(得分:1)

您需要添加转换器并在spring中注册。 baeldung.com/spring-mvc-custom-data-binder

答案 3 :(得分:1)

您可以将您的PaymentTransactionsDTO“ created_at”属性键入为字符串,并使用转换器将字符串转换为LocalDate(实体“ PaymentTransactions”的created_at属性的类型)

@Component
public class PaymentTransactionsConverter implements Converter<PaymentTransactionsDTO, PaymentTransactions> {

    @Override
    public PaymentTransactions convert(PaymentTransactionsDTO paymentTransactionsDTO) {

        PaymentTransactions paymentTransactions = new PaymentTransactions();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        ...

        paymentTransactions.setCreated_at(LocalDate.parse(paymentTransactionsDTO.getCreated_at(), formatter));

        return paymentTransactions;
    }
}