【发布时间】:2025-12-18 10:35:01
【问题描述】:
我已经看到How to use LocalDateTime RequestParam in Spring? I get "Failed to convert String to LocalDateTime",但我仍然遇到问题。
所以编写一个带有 rest api 的 Spring Boot 应用程序。
控制器是
@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 "授权: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":"内部 服务器错误","message":"值不能为空!;嵌套异常是 java.lang.IllegalArgumentException:值不能是 空!","路径":"/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
请求和响应完成
2019-12-19 11:05:21.668 错误 21113 --- [nio-8453-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] :Servlet.service() for 带有路径 [] 的上下文中的 servlet [dispatcherServlet] 引发异常 [请求处理失败;嵌套异常是 org.springframework.dao.InvalidDataAccessApiUsageException:值必须 不能为空!;嵌套异常是 java.lang.IllegalArgumentException: 值不能为空!] 有根本原因
java.lang.IllegalArgumentException:值不能为空!在 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。使用不同的模式只是为了看看一个是否有效。
【问题讨论】:
-
我怀疑问题出在你的 DateTimeFormat 上,你的参数很可能是 null
标签: java spring-boot