现在Spring boot大行其道,如何在Spring boot项目中使用JDK8中的日期时间类型接收请求、响应请求呢,今天我们简单介绍一下。
首先,我们来看一下一个普通Spring boot项目的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.RELEASE</version><relativePath/></parent><groupId>com.qwfys.sample</groupId><artifactId>jsr310-spring-boot</artifactId><version>0.0.1-SNAPSHOT</version><name>jsr310-spring-boot</name><description>jsr310 Demo project for Spring Boot</description><properties><java.version>1.8</java.version><swagger.starter.version>1.9.1.RELEASE</swagger.starter.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-starter-json</artifactId></dependency>--><dependency><groupId>com.spring4all</groupId><artifactId>swagger-spring-boot-starter</artifactId><version>${swagger.starter.version}</version></dependency><!--<dependency>--><!-- <groupId>org.mybatis.spring.boot</groupId>--><!-- <artifactId>mybatis-spring-boot-starter</artifactId>--><!-- <version>2.1.2</version>--><!--</dependency>--><!--<dependency>--><!-- <groupId>mysql</groupId>--><!-- <artifactId>mysql-connector-java</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>
考虑到JDK8中的日期功能是基于JSR310规范提交给JCP组织的,而在Spring boot 2.2.x中,这部分功能已经融合到spring-boot-starter-json
中去了,所以,我们要在项目中使用,只要追加如下内容即可。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-json</artifactId></dependency>
接下来在项目中就可以正常使用了。示例如下:
HahaController
package com.example.demo.abc.controller;import com.example.demo.abc.business.spec.HahaBusiness;
import com.example.demo.abc.domain.param.FindAllHahaParam;
import com.example.demo.abc.domain.vo.HahaVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@Api(tags = "Haha管理")
@RestController
@Slf4j
public class HahaController {
@AutowiredHahaBusiness hahaBusiness;@ApiOperation(value = "查询Haha记录")@PostMapping(value = "/abc/hahas/list")public List<HahaVo> findAll(FindAllHahaParam param) {
List<HahaVo> hahaVoList = hahaBusiness.findAll(param);return hahaVoList;}
}
FindAllHahaParam
package com.example.demo.abc.domain.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;@ApiModel("查询Haha参数")
@Data
public class FindAllHahaParam {
/*** 当前页*/@ApiModelProperty(value = "当前页")Integer currentPage = 1;/*** 每页条数*/@ApiModelProperty(value = "每页条数")Integer pageSize = 20;/*** 开始日期*/@ApiModelProperty(value = "开始日期")@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate startTime;/*** 截止日期*/@ApiModelProperty(value = "截止日期")@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate endTime;/*** 开始日期*/@ApiModelProperty(value = "开始日期2")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime startTime2;/*** 截止日期*/@ApiModelProperty(value = "截止日期2")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime endTime2;
}
如果提交参数是一个日期类型,需要用LocalDate类型的参数接收,如果提交的是一个日期日间类型,需要用LocalDateTime类型接收。
package com.example.demo.abc.domain.vo;import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;import java.time.LocalDateTime;@ApiModel("Haha")
@Data
public class HahaVo {
/*** 主键*/@ApiModelProperty(value = "主键")private String id;/*** UID*/@ApiModelProperty(value = "UID")private String uid;/*** 名称*/@ApiModelProperty(value = "名称")private String name;/*** 创建时间*/@ApiModelProperty(value = "创建时间")@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")LocalDateTime createTime;/*** 更新时间*/@ApiModelProperty(value = "更新时间")@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")LocalDateTime updateTime;/*** 名称*/@ApiModelProperty(value = "备注")private String remark;}
如果期望用LocalDate、LocalDateTime生成返回结果,可以添加注解@JsonFormat,其中,指定pattern返回数据的格式,timezone指定时区。
HahaBusiness
package com.example.demo.abc.business.spec;import com.example.demo.abc.domain.param.FindAllHahaParam;
import com.example.demo.abc.domain.vo.HahaVo;import java.util.List;public interface HahaBusiness {
List<HahaVo> findAll(FindAllHahaParam param);
}
HahaBusinessImpl
package com.example.demo.abc.business.impl;import com.example.demo.abc.business.spec.HahaBusiness;
import com.example.demo.abc.domain.param.FindAllHahaParam;
import com.example.demo.abc.domain.vo.HahaVo;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;@Service
public class HahaBusinessImpl implements HahaBusiness {
@Overridepublic List<HahaVo> findAll(FindAllHahaParam param) {
List<HahaVo> vos = new ArrayList<>();HahaVo vo = new HahaVo();vo.setId("1");vo.setUid("11");vo.setName("haha");vo.setCreateTime(LocalDateTime.now());vo.setUpdateTime(LocalDateTime.now());vo.setRemark("honghong");vos.add(vo);return vos;}
}
application.yml
server:port: 10000
接下来,我们使用IntelliJ Idea的HttpClient工具演示如何提交请求。
dia-rest-api.http
POST http://127.0.0.1:10000/abc/hahas/list
Content-Type: application/x-www-form-urlencodedcurrentPage=1&endTime=2021-08-12&endTime2=2021-08-12 09:23:34&pageSize=100&startTime=2021-07-28&startTime2=2021-08-12 12:23:34###
response
POST http://127.0.0.1:10000/abc/hahas/listHTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Thu, 29 Jul 2021 10:09:06 GMT
Keep-Alive: timeout=60
Connection: keep-alive[{"id": "1","uid": "11","name": "haha","createTime": "2021-07-29","updateTime": "2021-07-29","remark": "honghong"}
]Response code: 200; Time: 9056ms; Content length: 109 bytes
项目代码放在了githut上了,如果需要可以自行下载。