如何在spring boot中使用JDK8中的LocalDate

原创
07/29 14:02
阅读数 0

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

追加

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </dependency>
package com.example.param;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * xxx
 */
@ApiModel("xxx")
@Data
public class XxxParam {

    /**
     * 当前页
     */
    @ApiModelProperty(value = "当前页")
    Integer currentPage = 1;

    /**
     * 每页条数
     */
    @ApiModelProperty(value = "每页条数")
    Integer pageSize = 20;

    /**
     * 开始日期
     */
    @ApiModelProperty(value = "开始日期")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
	//@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    LocalDate startTime;

    /**
     * 截止日期
     */
    @ApiModelProperty(value = "截止日期")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
	//@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    LocalDate endTime;
	
	
    /**
     * 开始日期
     */
    @ApiModelProperty(value = "开始日期"2)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    LocalDateTime startTime2;

    /**
     * 截止日期
     */
    @ApiModelProperty(value = "截止日期2")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    LocalDateTime endTime2;
}

IntelliJ Idea HttpClient

http-client.env.json

{
  "alpha": {
    "dia-host": "http://127.0.0.1:15000/dia/",
    "inv-host": "http://127.0.0.1:14000/inv/",
    "sys-host": "http://127.0.0.1:13000/sys/",
    "access-token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJVSUQiOiIxNDE1MTIyMjg4Mjg3MjIzODk5IiwiSElEIjoiMTIzNDU2IiwiVU5hbWUiOiLliJjmlofnp5EiLCJ0aW1lIjoiMjktMTAtMTgtNDAiLCJSSUQiOiIxNDA5ODQ1MjY2MDczODg2NzIxIiwiZXhwIjoyNjI3NDgxOTIwLCJESUQiOiI4In0.iPDrOHYYRVbjs2dFZLZPHgTa2CPMHTEHxslpzBN6giQ"
  },
  "beta": {
    "dia-host": "http://127.0.0.1:25000/dia/",
    "inv-host": "http://127.0.0.1:24000/inv/",
    "sys-host": "http://127.0.0.1:23000/sys/",
    "access-token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJVSUQiOiIxNDE1NTEwMDk3NDMzNjE2Mzg0IiwiSElEIjoiMTIzNDU2IiwiVU5hbWUiOiJMV0siLCJ0aW1lIjoiMjctMTMtNDgtMDciLCJSSUQiOiIxNDEwMjE2MzY1MTU3NzY5MjE2IiwiZXhwIjoyNjI3MzEwODg3LCJESUQiOiI3In0.XMoP-92WRC2yprHaB6pIuCrM1ffgCzl4OyzOuBxGb8o"
  }
}

dia-rest-api.http

POST http://{{inv-host}}/xxx/yyyy/list
Content-Type: application/x-www-form-urlencoded
Access-Token: {{access-token}}

startTime=&endTime=&pageSize=100&currentPage=1
###

POST http://{{inv-host}}/xxx/yyyy/list
Content-Type: application/x-www-form-urlencoded
Access-Token: {{access-token}}

startTime=2021-07-27&endTime=2021-07-27&startTime2=2021-07-27 09:24:51&endTime2=2021-07-27 09:04:36&pageSize=100&currentPage=1
###

如果期望用LocalDate、LocalDateTime生成返回结果,可以添加注解@JsonFormat,其中,指定pattern返回数据的格式,timezone指定时区。

需要特别说明的是,如果提交参数是一个日期类型,需要用LocalDate类型的参数接收,如果提交的是一个日期日间类型,需要用LocalDateTime类型接收。

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部