简体   繁体   English

使用JSON表示和链接资源的JAX-RS RESTful API

[英]JAX-RS RESTful API working with JSON representations and linking resources

Sometimes it's confusing how I should link resources within a RESTful API, consider for example the entities: 有时,我应该如何在RESTful API中链接资源感到困惑,例如考虑以下实体:

Profile (Users can create business profiles with address, details, etc..) 个人资料(用户可以使用地址,详细信息等创建业务个人资料。)

Plan (Already persisted in app's DB, created by administrators) 计划(已保留在管理员创建的应用程序数据库中)

The request to create a Profile looks like: 创建配置文件的请求如下所示:

POST /profiles
{
  "name": "Business name",
  "address": "The address",
  "phone": "0000000000" 
}

Now it is required that a Profile belongs to a Pricing Plan. 现在,要求配置文件属于定价计划。 So is it a good idea to do POST request like this with JSON? 那么用JSON进行这样的POST请求是一个好主意吗?

POST /profiles
{
  "name": "Business name",
  "address": "The address",
  "phone": "0000000000"
  "plan": {
    "id": 1
  }
}

and then load the plan by the provided id and associate it with the profile being created: 然后通过提供的ID加载计划,并将其与正在创建的配置文件相关联:

@POST
@Path("/profiles")
public Response createProfile(Profile profile) {

    // load plan resource from DB
    Plan plan = em.find(Plan.class, profile.getPlan().getId())

    // associate
    profile.setPlan(plan);

    // persist profile
    em.perist(profile);
}

The Profile entity: 个人档案实体:

@Entity
public class Profile implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "plan_id", nullable = false)
    private Plan plan;

    private String name
    ...

    // getters and setters

}

The Plan entity: 计划实体:

@Entity
public class Plan implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Column(nullable = false)
    private String name;

    @NotNull
    @Column(nullable = false, columnDefinition = "text")
    private String description;

    @NotNull
    @Column(nullable = false, precision = 8, scale = 2)
    private BigDecimal price;

    @NotNull
    @Column(nullable = false)
    private Integer days;

    @OneToMany(fetch = FetchType.LAZY, mappedBy="plan", cascade = CascadeType.ALL)
    private List<Profile> profiles;

    ...

}

In other words i am asking what I should pass to the request body in order to link a reference entity. 换句话说,我要问我应该传递给请求主体以链接参考实体。

I would like to believe that something like this is more reasonable: 我想相信这样的事情更合理:

POST /plans/1/profiles

but according to the REST and JSON semantics what would be the best option? 但是根据REST和JSON语义,什么是最佳选择?

I can also think of other ways such as providing the Plan id as a query param: 我还可以想到其他方法,例如将Plan ID作为查询参数提供:

POST /profiles?planId=1

I would say you need to do the following: 我会说您需要执行以下操作:

Create profile with 创建个人资料

POST: /profile

Assign plan with 分配计划

PUT: /profile/<profile_id>
{
   "name": <optional>,
   "plan_id": <optional>,
   ...
}

First thing is you separate create and update (POST/PUT). 第一件事是您分别创建和更新(POST / PUT)。 Another is you state profile ID for update in URL. 另一个是您说明要在URL中更新的配置文件ID。

You can set parameters you need to update in PUT request body and update only parameters which are set. 您可以在PUT请求正文中设置需要更新的参数,并且仅更新设置的参数。 Think it's fine with REST concept. 认为使用REST概念很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM