gpt4 book ai didi

java - REST 中的 @DateTimeFormat 变量给出 null

转载 作者:行者123 更新时间:2023-12-01 19:16:40 32 4
gpt4 key购买 nike

我见过How to use LocalDateTime RequestParam in Spring? I get "Failed to convert String to LocalDateTime"但我仍然遇到问题。

因此编写一个具有 REST API 的 Spring Boot 应用程序。

Controller 是

@RequestMapping(value = "/api/v1/climates/locationdates/{location}/{sdate}/{edate}", method = RequestMethod.GET)
public ResponseEntity<Object> getClimate(@PathVariable("location") long location,
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime sdate,
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime edate) {
System.out.println("CONTROLLER location is " + location + " start is " + sdate + " end is " + edate);
return new ResponseEntity<>(climateService.getClimatesByLocationAndDates(location, sdate, edate), HttpStatus.OK);
}

当我使用curl时(使用与创建记录相同的日期格式)

curl -k -w "\n" -H "Authorization: Bearer $TOKEN" https://mint191:8453/api/v1/climates/locationdates/1/2019-12-17T11:30:00/2019-12-17T11:55:00

我收到的回复

{"timestamp":"2019-12-19T11:05:21.707+0000","status":500,"error":"Internal Server Error","message":"Value must not be null!; nested exception is java.lang.IllegalArgumentException: Value must not be null!","path":"/api/v1/climates/locationdates/1/2019-12-17T11:30:00/2019-12-17T11:55:00"}

并在日志中

CONTROLLER location is 1 start is null end is null

Request and Response is completed

2019-12-19 11:05:21.668 ERROR 21113 --- [nio-8453-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Value must not be null!; nested exception is java.lang.IllegalArgumentException: Value must not be null!] with root cause

java.lang.IllegalArgumentException: Value must not be null! at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.2.2.RELEASE.jar!/:5.2.2.RELEASE]

模型是

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "climate_gen")
private long id;

private float temperature;
private float humidity;
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime date;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "location_id", nullable = false)
private Location location;

因此日期字符串 2019-12-17T11:55:00 不会转换为 LocalDatTime 对象。我做错了什么?

PS。使用了不同的模式只是为了看看其中一种是否有效。

最佳答案

将 @PathVariable 添加到 sdate 和 edate 以从 url 访问值,如下所示:

@RequestMapping(value = "/api/v1/climates/locationdates/{location}/{sdate}/{edate}", method = RequestMethod.GET)
public ResponseEntity<Object> getClimate(@PathVariable("location") long location,
@PathVariable("sdate") @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime sdate,
@PathVariable("edate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime edate) {

System.out.println("CONTROLLER location is " + location + " start is " + sdate + " end is " + edate);
return new ResponseEntity<>(climateService.getClimatesByLocationAndDates(location, sdate, edate), HttpStatus.OK);
}

关于java - REST 中的 @DateTimeFormat 变量给出 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59408772/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com