是否可以将路径变量和请求参数绑定到单个对象中?

时间:2019-04-22 09:58:01

标签: java spring-boot spring-mvc java-8

我正在使用路径变量和请求参数创建一个端点。 如何将路径变量和请求参数组合到一个对象中?我在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
}

2 个答案:

答案 0 :(得分:4)

创建模型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

答案 1 :(得分:0)

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();

    }
}

喜欢这样。它会起作用。