pip*_*lam 6 java json localdate
在主题中,我想将日期作为参数传递。我有这个依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的URL样子:
http://localhost:8080/userProducts/2?date=2019-3-29
Run Code Online (Sandbox Code Playgroud)
我的控制器类看起来像:
package trainingapp.userproduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import trainingapp.calculations.NutrientsCalculationFacade;
import trainingapp.historysystemofmeals.HistorySystemService;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@RestController
public class UserProductController {
private final NutrientsCalculationFacade userProductCalculationFacade;
private final UserProductFindOperationService userProductFindOperationService;
private final HistorySystemService historySystemService;
@Autowired
public UserProductController(NutrientsCalculationFacade userProductCalculationFacade, UserProductFindOperationService userProductFindOperationService, HistorySystemService historySystemService) {
this.userProductCalculationFacade = userProductCalculationFacade;
this.userProductFindOperationService = userProductFindOperationService;
this.historySystemService = historySystemService;
}
//yyyy-MM-dd
@GetMapping("/userProducts/{userID}")
public String getAllEatenSummedNutrientsByGivenIDInParticularDay(@PathVariable int userID,
@RequestParam("date")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
return historySystemService.getAllEatenSummedNutrientsByGivenIDInParticularDay(userID, date);
}
}
Run Code Online (Sandbox Code Playgroud)
我有错误:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我应该更改或添加什么?我试图通过以下方式解决这个问题: How to use LocalDateTime RequestParam in Spring? 我收到“无法将字符串转换为 LocalDateTime”,但它没有用。
2019-3-29是一个错误的输入并且400状态代码是准确的。请参阅DateTimeFormat.ISO#DATE文档:
最常见的 ISO 日期格式
yyyy-MM-dd,例如“2000-10-31”。
因此,要匹配上述格式,您的输入应该是2019-03-29。
| 归档时间: |
|
| 查看次数: |
16202 次 |
| 最近记录: |