Spring MVC:使用复合键进行PUT和DELETE

时间:2017-08-24 13:54:57

标签: spring spring-mvc spring-boot

我使用Spring Boot创建一个Web应用程序,该应用程序使用JPA保留FooBar实体。

我有一些html页面在我的控制器上执行AJAX请求。 请求与此实体有关:

@Entity
@Table(name = "FOO_TABLE")
public class FooBar {

  @EmbeddedId
  private FooBarId id;

  @Column(name = "ADDRESS")
  private String address;

  @Column(name = "COLOR")
  private String color;  
}

它使用复合键:

@Embeddable
public class FooBarId {

  @NotNull
  @Column(name = "NAME")
  private String name;

  @NotNull
  @Column(name = "TXT_ADR_MAIL")
  private String email;
}

POST没问题:

@PostMapping
public ResponseEntity<Void> postFoobar(FooBar fb){
  repo.save(fb)
  return new ResponseEntity<>(HttpStatus.CREATED);
}

问题:

如何执行PUTGETDELETE?我不知道怎么能这样做,因为我曾经处理过一个简单的id。 那么可以用复合键执行这些操作吗?

修改1:

  • 我的表格中没有id列。我无法改变这张桌子。

  • 到目前为止我尝试了:对于DELETE,我将整个实体传递给控制器​​,然后根据Key搜索要删除的实体。 对于PUTGET(单人获取),我不知道从哪里开始。

问候。

1 个答案:

答案 0 :(得分:1)

假设您将DTO和实体分开。因此,在PUT / DELETE中,只需将正文放入您的请求即可。对于GET,您可以尝试使用POST来获取数据,因为您的ID与电子邮件和名称相当复杂。因此,将该ID放入Body并执行Post查询数据。这是我试过的:

- FooBarDTO

public class FooBarDTO {
    private String name;
    private String email;
    private String address;
    private String color;

    public FooBarDTO(){}
    /**
     * @param name
     * @param email
     * @param id
     * @param address
     * @param color
     */
    public FooBarDTO(String name, String email, String address, String color) {
        this.name = name;
        this.email = email;
        this.address = address;
        this.color = color;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }
    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }
    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }
    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }
    /**
     * @return the color
     */
    public String getColor() {
        return color;
    }
    /**
     * @param color the color to set
     */
    public void setColor(String color) {
        this.color = color;
    }


}

- FooBarIdDTO

public class FooBarIdDTO {
    private String name;
    private String email;
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }
    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

}

---在Controller中添加以下方法:

@Autowired
    private FooBarRepository repo;

    // test FooBar
    @RequestMapping(value = "/foo", method = RequestMethod.POST)
    public ResponseEntity<?> postFoo(@RequestBody FooBarDTO body){
        FooBarId id = new FooBarId();
        id.setEmail(body.getEmail());
        id.setName(body.getName());

        FooBar fooBar = new FooBar();
        fooBar.setId(id);
        fooBar.setAddress(body.getAddress());
        fooBar.setColor(body.getColor());
        repo.save(fooBar);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    //test PUT
    @RequestMapping(value = "/foo", method = RequestMethod.PUT)
    public ResponseEntity<?> putFoo(@RequestBody FooBarDTO body){
        FooBarId id = new FooBarId();
        id.setEmail(body.getEmail());
        id.setName(body.getName());

        FooBar fooBar = new FooBar();
        fooBar.setId(id);
        fooBar.setAddress(body.getAddress());
        fooBar.setColor(body.getColor());
        repo.save(fooBar);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    //test Delete FooBar
    @RequestMapping(value = "/foo", method = RequestMethod.DELETE)
    public ResponseEntity<?> deleteFoo(@RequestBody FooBarIdDTO body){
        FooBarId id = new FooBarId();
        id.setEmail(body.getEmail());
        id.setName(body.getName());
        repo.delete(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    // test FooBar
        @RequestMapping(value = "/getFoo", method = RequestMethod.POST)
        public ResponseEntity<?> getFoo(@RequestBody FooBarIdDTO body){
            FooBarId id = new FooBarId();
            id.setEmail(body.getEmail());
            id.setName(body.getName());
            FooBar result = repo.findOne(id);
            return ResponseEntity.ok(result);
        }

结果为图像

enter image description here