gpt4 book ai didi

java - 如何在 Spring Boot 的路径变量中传递本地日期?

转载 作者:行者123 更新时间:2023-12-01 14:32:46 31 4
gpt4 key购买 nike

我正在编写 REST 服务。

我想按在@Path 变量中传递的日期获取所有记录。

我怎样才能做到这一点?

我试图做的事情:

型号分类:

@Entity
@Table(name = "test")
public class Test {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

private String name;


@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate beginDate;


@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate endDate;


private String activity;
}

存储库:
@Repository
public interface TestRepository extends JpaRepository<Test, Integer> {

List<Test> findAllByName(String name);

List<Test> findAllByBeginDate(LocalDate date);
}

服务:
@Service
public class TestService {

@Autowired
private final TestRepository testRepository;

public TestService(TestRepository testRepository) {
this.testRepository = testRepository;
}

public List<Test> getAllTestsByBeginDate(LocalDate date) {
return testRepository.findAllByBeginDate(date);
}
}

Controller :
@RestController
@RequestMapping("/api/v1/")
public class TestController {

@GetMapping("test/all/{date}")
public List<Test> getAllTestsByBeginDate(@PathVariable ("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {


return testService.getAllTestsByBeginDate(date);
}
}

当我通过这样的日期时,我收到错误:

Postman Get Method

最佳答案

这应该工作

@RestController
@RequestMapping("/api/v1/")
public class TestController {

@GetMapping("test/all/{date}")
public List<Test> getAllTestsByBeginDate(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {


return testService.getAllTestsByBeginDate(date);
}
}

或此 link会有所帮助

关于java - 如何在 Spring Boot 的路径变量中传递本地日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60371954/

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