Springboot/Spring JPA - 具有多个 GetMapping 方法的控制器
Springboot/Spring JPA - Controller with multiple GetMapping methods
我有几个 SQL 查询从同一个 table 获取数据,只是过滤方式不同。例如:
SELECT * FROM CAR WHERE CAR_NUM <> 111
SELECT * FROM CAR WHERE SELL_DATE BETWEEN '2020-01-01' AND '2021-12-15' //These are DB2 TIMESTAMP fields (e.g. '2021-12-15 12:45:33')
SELECT * FROM CAR WHERE ...
...
...
我有大约 10 个查询,每个查询都针对 CAR
数据 table,但使用不同的 WHERE
子句以不同的方式过滤数据。
我使用 Spring JPA 实现了 CarController
、CarService
、CarRepository
。我的控制器目前有 2 个 @GetMapping
方法,我计划添加更多 @GetMapping
方法来涵盖上面的所有 SQL 查询
@RestController
@RequestMapping("/cars")
public class CarController {
@GetMapping
public List<CarResponse> getAllCars() {
// handle 1st query above and return all cars except one with CAR_NUM=111
List<CarResponse> cars = carRepository.getAllCarsExceptTrippleOne();
return cars;
}
@GetMapping
public List<CarResponse> getAllCarsSoldBetweenDates(@RequestParam Map<Date, Date> dateRange) {
// handle 2nd query above and return all cars sold btw 2 dates. My table holds DB2 TIMESTAMP fields.
List<CarResponse> cars = carRepository.getAllCarsSoldBetweenDates(dateRange.get("startDate"), dateRange.get("endDate"));
return cars;
}
}
但是,我收到如下错误:
java.lang.IllegalStateException: Ambiguous mapping. Cannot map
'carController' method
com.me.carController#getAllCarsSoldBetweenDates(Map) to {GET [/cars],
produces [application/json]}: There is already 'carController' bean
method com.me.carController#getAllCars() mapped.
我不确定我缺少什么?
您为两种方法都指定了 @GetMapping()
。这将两种方法与一个端点 /cars
绑定在一起。每个控制器方法都应该有与之关联的唯一映射。
@GetMapping
public List<CarResponse> getAllCars() {}
@GetMapping("/soldbetween")
public List<CarResponse> getAllCarsSoldBetweenDates(@RequestParam Map<Date, Date> dateRange) {}
您需要告诉 spring 当您想要获得所有汽车时要调用哪个 GET
映射,当您想要在日期之间售出汽车时要使用哪个映射。
不要理会第一个方法,将第二个方法更改为如下所示:
@GetMapping("/sold/{startDate}/{endDate}")
public List<CarResponse> getAllCarsSoldBetweenDates(@PathVariable Long startDate, @PathVariable Long endDate) {
// do validation of the variables, if variables cannot be converted to dates or something
try{
Date startDateObj = new Date(startDate);
Date endDateObj = new Date(endDate);
// handle 2nd query above and return all cars sold btw 2 dates
List<CarResponse> cars = carRepository.getAllCarsSoldBetweenDates(startDateObj, endDateObj);
return cars;
}catch( Exception e ){
// .. handle exception and return empty list?
return Collections.emptyList();
}
}
然后,要调用此方法,您可以通过调用服务器并传递参数来调用,例如:
https://<server>:<port>/cars/sold/<startDateAsLong>/<endDateAsLong>
我有几个 SQL 查询从同一个 table 获取数据,只是过滤方式不同。例如:
SELECT * FROM CAR WHERE CAR_NUM <> 111
SELECT * FROM CAR WHERE SELL_DATE BETWEEN '2020-01-01' AND '2021-12-15' //These are DB2 TIMESTAMP fields (e.g. '2021-12-15 12:45:33')
SELECT * FROM CAR WHERE ...
...
...
我有大约 10 个查询,每个查询都针对 CAR
数据 table,但使用不同的 WHERE
子句以不同的方式过滤数据。
我使用 Spring JPA 实现了 CarController
、CarService
、CarRepository
。我的控制器目前有 2 个 @GetMapping
方法,我计划添加更多 @GetMapping
方法来涵盖上面的所有 SQL 查询
@RestController
@RequestMapping("/cars")
public class CarController {
@GetMapping
public List<CarResponse> getAllCars() {
// handle 1st query above and return all cars except one with CAR_NUM=111
List<CarResponse> cars = carRepository.getAllCarsExceptTrippleOne();
return cars;
}
@GetMapping
public List<CarResponse> getAllCarsSoldBetweenDates(@RequestParam Map<Date, Date> dateRange) {
// handle 2nd query above and return all cars sold btw 2 dates. My table holds DB2 TIMESTAMP fields.
List<CarResponse> cars = carRepository.getAllCarsSoldBetweenDates(dateRange.get("startDate"), dateRange.get("endDate"));
return cars;
}
}
但是,我收到如下错误:
java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'carController' method com.me.carController#getAllCarsSoldBetweenDates(Map) to {GET [/cars], produces [application/json]}: There is already 'carController' bean method com.me.carController#getAllCars() mapped.
我不确定我缺少什么?
您为两种方法都指定了 @GetMapping()
。这将两种方法与一个端点 /cars
绑定在一起。每个控制器方法都应该有与之关联的唯一映射。
@GetMapping
public List<CarResponse> getAllCars() {}
@GetMapping("/soldbetween")
public List<CarResponse> getAllCarsSoldBetweenDates(@RequestParam Map<Date, Date> dateRange) {}
您需要告诉 spring 当您想要获得所有汽车时要调用哪个 GET
映射,当您想要在日期之间售出汽车时要使用哪个映射。
不要理会第一个方法,将第二个方法更改为如下所示:
@GetMapping("/sold/{startDate}/{endDate}")
public List<CarResponse> getAllCarsSoldBetweenDates(@PathVariable Long startDate, @PathVariable Long endDate) {
// do validation of the variables, if variables cannot be converted to dates or something
try{
Date startDateObj = new Date(startDate);
Date endDateObj = new Date(endDate);
// handle 2nd query above and return all cars sold btw 2 dates
List<CarResponse> cars = carRepository.getAllCarsSoldBetweenDates(startDateObj, endDateObj);
return cars;
}catch( Exception e ){
// .. handle exception and return empty list?
return Collections.emptyList();
}
}
然后,要调用此方法,您可以通过调用服务器并传递参数来调用,例如:
https://<server>:<port>/cars/sold/<startDateAsLong>/<endDateAsLong>