takeaway
For project purposes, @jsonformat does not work when formatting dates using Lombok.
use
Entity class
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
@Data
public class Ts {
private String id;
private String name;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date cTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date insertime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime;
}
Copy the code
test
Control layer
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@RequestMapping("/api")
@RestController
public class TsController {
@RequestMapping("/index")
public List<Ts> execute(a) {
Ts ts = new Ts();
ts.setAge(10);
ts.setCTime(new Date());
ts.setInsertime(new Date());
ts.setUpdateTime(new Date());
ts.setId(UUID.randomUUID().toString());
ts.setName("Millet");
Ts s = new Ts();
s.setAge(null);
s.setCTime(new Date());
s.setInsertime(new Date());
s.setUpdateTime(new Date());
s.setId(String.valueOf(Math.random()));
s.setName("Xiao wu");
List list = new ArrayList<>();
list.add(s);
list.add(ts);
returnlist; }}Copy the code
Send a request for data
The GET: HTTP://localhost:8080/api/index[{"id": "0.8696139330472303"."name": "Xiao wu"."cTime": "The 2020-10-27 10:28:09"."insertime": "The 2020-10-27 10:28:09"."updateTime": "The 2020-10-27 10:28:09"."ctime": "The 2020-10-27 T02:28:09. 045 + 0000"
},
{
"id": "39547d17-d214-4fee-8b07-eb39e8e6edb3"."name": "Millet"."age": 10."cTime": "The 2020-10-27 10:28:09"."insertime": "The 2020-10-27 10:28:09"."updateTime": "The 2020-10-27 10:28:09"."ctime": "The 2020-10-27 T02:28:09. 045 + 0000"
},
{
"id": "10"."address": "Hangzhou"}]Copy the code
conclusion
When using Lombok’s defined entity classes, the second letter of the attribute is not capitalized, otherwise duplicate capitalization results will occur (such as cTime, cTime, and cTime data). Uppercase letters except the second letter are supported.
END
chaohen
:www.yuque.com/heioky
Do ~