我正在创建一个带有路径变量和请求参数的端点。如何将路径变量和请求参数组合成单个对象?我正在使用Java 8的springboot 2。
@RequestMapping(path = "/schedules")
public class SchedulesController {
@GetMapping("/{area}/{subarea}")
public MyObject getFlight(@PathVariable("area") String area, @PathVariable("subarea") String subarea,
MyModel model) {
...
return new MyObject();
}
}
@Data
public class MyModel {
LocalDate datestamp,
String leadName,
String viceLeadName
}
我看了spring文档,但好像找不到如何将PathVariable合并到MyModel的方法。
以下是我的控制器示例(未包含该对象)。
@RequestMapping(path = "/schedules")
public class SchedulesController {
@GetMapping("/{area}/{subarea}")
public MyObject getFlight(@PathVariable("area") String area, @PathVariable("subarea") String subarea,
@RequestParam(value = "datestamp", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate datestamp,
@RequestParam(value = "leadName", required = false) String leadName,
@RequestParam(value = "viceLeadName", required = false) String viceLeadName) {
...
return new MyObject();
}
}
是否可以实现以下功能?将路径变量和请求参数放在MyModel对象中?
@RequestMapping(path = "/schedules")
public class SchedulesController {
@GetMapping("/{area}/{subarea}")
public MyObject getFlight(MyModel model) {
...
return new MyObject();
}
}
@Data
public class MyModel {
String area,
String subArea,
LocalDate datestamp,
String leadName,
String viceLeadName
}