简体   繁体   English

Spring REST API对连接表的响应

[英]Spring REST API Response for Junction Tables

I have a MySQL DB and a SpringBoot application running on Tomcat. 我有一个在Tomcat上运行的MySQL数据库和一个SpringBoot应用程序。 This is a personal project of mine and my first experience designing REST services. 这是我的个人项目,也是我设计REST服务的首次经验。 I have been trying to create my POJOs to match the database, but this particular object is creating some doubts. 我一直在尝试创建与数据库匹配的POJO,但是这个特定的对象引起了一些疑问。

Here are the primary database tables: 这是主数据库表:

  • player (id, description) 播放器(ID,说明)
  • campaign (id, description) 广告系列(ID,说明)
  • resource (id, description, resource_type_id) 资源(ID,描述,resource_type_id)
  • resource_type (id, description) resource_type(ID,说明)
  • component (id, description) 组件(ID,说明)

...and following junction tables: ...以及以下联结表:

  • player_resource (id, player_id, resource_id): a player may have many resources, and a resource may belong to many players. player_resource(id,player_id,resource_id):一个玩家可能拥有许多资源,并且一个资源可能属于许多玩家。
  • campaign_resource (id, campaign_id, resource_id): a campaign may have many resources and a resource may belong to many campaigns. campaign_resource(id,campaign_id,resource_id):一个活动可能有很多资源,并且一个资源可能属于许多活动。
  • resource_component (id, resource_id, component_id): a resource may have many components and a component may belong to many resources resource_component(id,resource_id,component_id):一个资源可能具有许多组件,而一个组件可能属于许多资源

I'm sort of lost on how I should be designing my JSON response and the corresponding POJO(s) for the Resource object specifically. 我对于应该如何设计JSON响应以及为Resource对象专门设计相应的POJO感到迷茫。

What I'm thinking so far is to have three separate URI: 到目前为止,我在想的是拥有三个单独的URI:

  1. .../api/resources/{uuid} : Returns the specific Resource. ... / api / resources / {uuid} :返回特定的资源。
  2. .../api/campaigns/{uuid}/resources : Returns which resources are available in the supplied campaign. ... / api / campaigns / {uuid} / resources :返回提供的广告系列中可用的资源。 As an aside, players exist within the scope of a campaign so this particular API ultimately determines what's available to a player to obtain in the course of their existence. 顺便说一句,玩家存在于战役范围内,因此这个特定的API最终决定了玩家在其生存过程中可获得的东西。
  3. .../api/players/{uuid}/resources : Returns which resources the player currently has in their inventory. ... / api / players / {uuid} / resources :返回玩家当前在其清单中拥有的资源。

I created a single POJO for Resource: 我为Resource创建了一个POJO:

public class Resource {

    private String resourceId;
    private String shortDescription;
    private String longDescription;
    private ResourceType resourceType;
    private List<Component> components;

    // getters and setters

}

I created two additional POJOs for CampaignResource and PlayerResource: 我为CampaignResource和PlayerResource创建了两个附加的POJO:

public class CampaignResource {

    private String campaignResourceId;
    private Resource resource;

    // getters and setters

}

public class PlayerResource {

    private String playerResourceId;
    private Resource resource;

    // getters and setters

}

I expect URI #1 and #2 to return the same layout, but I have redundancy concerns with URI #3. 我希望URI#1和#2返回相同的布局,但是我对URI#3感到多余。 URI #2 may return ten Resources which is fine and expected, but URI #3 could return hundreds depending on what a player is carrying around. URI#2可能会返回十个正常且预期的资源,但URI#3可能会返回数百个,具体取决于玩家携带的物品。

The only information I really need when calling URI #3 are the player_resource_id, resource_id, and resource_type_id fields. 调用URI#3时,我真正需要的唯一信息是player_resource_id,resource_id和resource_type_id字段。 I do not need to know what components comprise a resource because that information is brought back in a much more manageable fashion using either URI #1 or URI #1. 我不需要知道什么组件构成了资源,因为使用URI#1或URI#1以更易于管理的方式带回了该信息。

I'm afraid to create separate "base" Resource POJOs since I feel obligated to follow some form of canonical modeling. 我害怕创建单独的“基础”资源POJO,因为我觉得必须遵循某种形式的规范建模。 Could I use something like a JsonView and separate Resource RowMappers based on whether it's URI #1, #2, or #3? 是否可以使用JsonView之类的东西,并根据URI#1,#2或#3分开使用Resource RowMappers? I could implement some form of paging, but it's still a lot of redundant data. 我可以实现某种形式的分页,但是仍然有很多冗余数据。 How would a veteran REST developer approach this? 经验丰富的REST开发人员将如何处理这个问题?

Why do you need to create separate classes that contain resource for each REST API. 为什么需要创建包含每个REST API资源的单独类。 Just return one Resource for #1 and list of Resource(s) for #2 and #3. 只需为#1返回一个资源,为#2和#3返回一个或多个资源列表。

I think that if you want to know about RESTful API's this post will help a lot. 我认为,如果您想了解RESTful API的话, 这篇文章会有所帮助。 Specifically about what you are asking, look in the topic Limiting which fields are returned by the API : 专门针对您的要求,查看主题“ 限制API返回哪些字段”

Use a fields query parameter that takes a comma separated list of fields to include. 使用字段查询参数,该参数采用逗号分隔的字段列表。

For Example: 例如:

GET /tickets?fields=id,subject,customer_name,updated_at&state=open&sort=-updated_at

Here you can find how Google, Facebook and LinkdIn do. 在这里,您可以找到Google,Facebook和LinkdIn的工作方式。

[]s []中

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

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