Программисты JAVA общаются здесь
Anonymous
Как передать локальную дату в переменной пути в Spring Boot?
Сообщение
Anonymous » 10 апр 2024, 10:13
Я пишу службу 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 {
List findAllByName(String name);
List findAllByBeginDate(LocalDate date);
}
Сервис:
Код: Выделить всё
@Service
public class TestService {
@Autowired
private final TestRepository testRepository;
public TestService(TestRepository testRepository) {
this.testRepository = testRepository;
}
public List getAllTestsByBeginDate(LocalDate date) {
return testRepository.findAllByBeginDate(date);
}
}
Контроллер:
Код: Выделить всё
@RestController
@RequestMapping("/api/v1/")
public class TestController {
@GetMapping("test/all/{date}")
public List getAllTestsByBeginDate(@PathVariable ("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
return testService.getAllTestsByBeginDate(date);
}
}
Когда я передаю такую дату, я получаю ошибки:
Подробнее здесь:
https://stackoverflow.com/questions/603 ... pring-boot
1712733233
Anonymous
Я пишу службу REST. Я хочу получить все записи по дате, которые я передаю в переменной @Path. [b]Как я могу это сделать?[/b] Что я пытался сделать: Класс модели: [code]@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; } [/code] Репозиторий: [code]@Repository public interface TestRepository extends JpaRepository { List findAllByName(String name); List findAllByBeginDate(LocalDate date); } [/code] Сервис: [code]@Service public class TestService { @Autowired private final TestRepository testRepository; public TestService(TestRepository testRepository) { this.testRepository = testRepository; } public List getAllTestsByBeginDate(LocalDate date) { return testRepository.findAllByBeginDate(date); } } [/code] Контроллер: [code]@RestController @RequestMapping("/api/v1/") public class TestController { @GetMapping("test/all/{date}") public List getAllTestsByBeginDate(@PathVariable ("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) { return testService.getAllTestsByBeginDate(date); } } [/code] Когда я передаю такую дату, я получаю ошибки: [img]https://i.stack.imgur.com/wlxDK.jpg[/img] Подробнее здесь: [url]https://stackoverflow.com/questions/60371954/how-to-pass-local-date-in-path-variable-in-spring-boot[/url]