【问题标题】:Spring MVC : PUT and DELETE with a composite keySpring MVC:使用复合键 PUT 和 DELETE
【发布时间】:2017-08-24 13:54:57
【问题描述】:

我正在使用 Spring Boot 创建一个使用 JPA 持久化 FooBar 实体的 Web 应用程序。

我有一些 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,我将整个实体传递给控制器​​,然后根据密钥搜索要删除的实体。 对于PUTGET(单get),不知从何说起。

问候。

【问题讨论】:

  • 你尝试了什么?
  • @Nikolay 请参考我的编辑。

标签: spring spring-mvc spring-boot


【解决方案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;
    }

}

--- 在控制器中添加这些方法:

@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);
        }

结果为图片

【讨论】:

    猜你喜欢
    • 2018-06-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    相关资源
    最近更新 更多