在 Spring Data REST+HATEOAS 中删除 REST 上的关联 [英] Deleting an association over REST in Spring Data REST+HATEOAS

查看:64
本文介绍了在 Spring Data REST+HATEOAS 中删除 REST 上的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何通过 REST 调用删除多对多关联.我能够创建记录并关联它们,但不知道如何删除.

我有一个 Spring Boot 项目,我在其中使用 REST 和 HATEOAS 绕过服务和控制器并直接公开我的存储库.

I have a Spring Boot project where i'm using REST and HATEOAS to by pass Services and Controllers and expose my Repository directly.

我有一个用户模型/域类

I have a User Model/Domain class

@Entity
@Table(name = "usr")
public class User implements Serializable {

private static final long serialVersionUID = 1L;

@Version
private long version = 0;

@Id
@GeneratedValue(generator="optimized-sequence")
private Long id;

@Column(nullable = false, unique = true, length = 500)
@Size(max = 500)
private String userName;

@Column(nullable = false, length = 500)
@Size(max = 500)
private String firstName;

@Column(nullable = false, length = 500)
@Size(max = 500)
private String lastName;

@ManyToMany(    fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable( name="user_role",
            joinColumns={ @JoinColumn(  name = "user_id", 
                                        nullable = false
                                    ) 
                        }, 
            inverseJoinColumns={ @JoinColumn(   name="role_id", 
                                                nullable=false
                                            ) 
                                }
)
private Set<Role> roles = new HashSet<Role>(0);

...Getters/Setters Below...

如您所见,我有一个 roles 成员,它与 Role 类进行多对多关联,其代码如下:

As as you can see, I have a roles member that is Many-To-Many association with Role class, of which the code is such:

@Entity
public class Role {

@Id
@GeneratedValue(generator="optimized-sequence")
private Long id;

@Column(nullable = false)
private String name;

@Column(nullable = false)
private String description;

...Getters/Setters Below...

我的存储库如下所示:

用户存储库

public interface UserRepository extends 
        JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    List<User> findByUserName(String username);

}

角色存储库

public interface RoleRepository 
        extends JpaRepository<Role, Long> {

}

现在,一切都很好.当我从浏览器访问项目根目录时,我以 JSON+HAL 格式获取存储库索引/目录.太棒了.

Now, all is well. When I access the project root from a browser, I get the repository index/directory in JSON+HAL format. Wonderful.

(注意,我从下面的测试中删除了 http://部分,因为 StackOverflow 将其计入我的链接配额)

(Note I'm remove the http:// part from the test below because StackOverflow is counting it towards my links quota)

我,使用 WizTools REST 客户端,HTTP.POST 到角色 ( localhost:8080/resttest/roles ) 存储库并创建一个新角色.成功,已创建角色 ID #4.

I, using WizTools REST Client, HTTP.POST to the Role ( localhost:8080/resttest/roles ) repository and create a new Role. Success, Role ID #4 created.

然后我 POST 到用户存储库以创建一个用户 ( localhost:8080/resttest/users ).成功,已创建用户 ID #7.

Then I POST to the User repository to create a User ( localhost:8080/resttest/users ). Success, User ID #7 created.

然后我 PUT 到用户存储库以创建与角色的关联:

Then I PUT to the User repository to create an association with the role:

PUT localhost:8080/resttest/users/7/roles
Content-type: uri-list
Body: localhost:8080/resttest/roles/4

太好了!协会作出.用户 9 现在与角色 4 相关联.

Great! Association made. User 9 is now associated with Role 4.

现在我终生无法弄清楚如何删除此关联.

Now I can't for the life of me figure out how to DELETE this association.

我使用与上述相同的命令发送 HTTP DELETE 而不是 PUT.

I'm sending an HTTP DELETE instead of PUT with the same command as above.

DELETE localhost:8080/resttest/users/7/roles
Content-type: uri-list
Body: localhost:8080/resttest/roles/4

我回来了:HTTP/1.1 405 Method Not Allowed

I get back: HTTP/1.1 405 Method Not Allowed

{
    "timestamp":1424827169981,
    "status":405,
    "error":"Method Not  Allowed",
    "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
    "message":"Request method 'POST' not supported"
}

推荐答案

虽然使用剩余元素创建 PUT 请求可以解决问题,但 DELETE 是一个可接受的删除命令一种关联资源,在大多数情况下更易于使用.

Although creating a PUT request with remaining elements does the trick, DELETE is an accepted command to delete an association resource and is in most cases easier to use.

就您的示例而言,这应该可行:

As for your example, this should work:

DELETE localhost:8080/resttest/users/7/roles/4

另外,在创建关联时,应在有效负载中包含 URI.您不需要在正文中写入整个 URL,这样就足够了:

On a separate note, when creating the association, it is expected to have URIs in the payload. You shouldn't need to write the whole URL in the body, this should be enough:

PUT localhost:8080/resttest/users/7/roles
Content-type: uri-list
Body: /roles/4

希望这会有所帮助.

这篇关于在 Spring Data REST+HATEOAS 中删除 REST 上的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆