是否可以将路径变量和请求参数绑定到单个对象中? [重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了是否可以将路径变量和请求参数绑定到单个对象中? [重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我正在创建一个带路径变量和请求参数的端点。如何将路径变量和请求参数组合到单个对象中?我正在使用带有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
}
答案
创建一个模型MyModel
class MyModel {
String area;
String subarea;
@DateTimeFormat(pattern = "yyyy-MM-dd")
LocalDate datestamp;
String leadname;
String viceleadname;
@Override
public String toString() {
return "MyModel [area=" + area + ", subarea=" + subarea + ", datestamp=" + datestamp + ", leadname=" + leadname
+ ", viceneadname=" + viceleadname + "]";
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getSubarea() {
return subarea;
}
public void setSubarea(String subarea) {
this.subarea = subarea;
}
public LocalDate getDatestamp() {
return datestamp;
}
public void setDatestamp(LocalDate datestamp) {
this.datestamp = datestamp;
}
public String getLeadname() {
return leadname;
}
public void setLeadname(String leadname) {
this.leadname = leadname;
}
public String getViceneadname() {
return viceleadname;
}
public void setViceneadname(String viceleadname) {
this.viceleadname = viceleadname;
}
请求映射与您提到的相同。
@GetMapping("/{area}/{subarea}")
public String getFlight(MyModel model) {
System.out.println(model);
return "success";
}
请求看起来像这样:
http://localhost:8080/us/ny?datestamp=2019-05-01&leadname=abc&viceleadname=xyz
另一答案
public class Location {
private String area;
private String subarea;
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getSubarea() {
return subarea;
}
public void setSubarea(String subarea) {
this.subarea = subarea;
}
}
@Controller
@RequestMapping("schedules")
public class SchedulesController {
@GetMapping("{area}/{subarea}")
public String getFlight(Location location ) {
String msg = String.format("location request. area: %s, subarea: %s",
location.getArea(), location.getSubarea());
..............................
return new MyObject();
}
}
这样做。它会工作。
以上是关于是否可以将路径变量和请求参数绑定到单个对象中? [重复]的主要内容,如果未能解决你的问题,请参考以下文章