RESTful 1-N Extra Relationships

I'm trying to learn how to write RESTful Java applications using Jersey and Hibernate, and I'm struggling to figure out how to handle the parent / child relationship type when sending data to the resource. I use JSON for data exchange, but I do not think this is especially important for my problem.

In the example in which I work with models, the relationship between employees and teams. An employee may or may not be a member of one team:

GET /team/ - returns a list of teams
POST /team/ - creates a new team
GET /team/1 - returns a list of employees in the team with the ID 1

GET /employee/ - returns a list of employees
POST /employee/ - creates a new employee
GET /employee/1 - returns details about the employee with the ID 1

For this, I have a few annotated Hibernate POJOs: one for the team and one for the employee, with a 1-N relationship between them (remember that the employee cannot be a member of the team!). The same POJOs are also annotated as @XmlRootElements so that JAXB allows me to pass them to / from the client as JSON.

The properties for the two objects are as follows:

Team
    Long id;
    String name;
    String desc;
    List<Employee> employees;

Employee
    Long id;
    Team team;
    String name;
    String phone;
    String email;

All still. But I'm struggling to figure out how to make an employee a member of a team during creation, simply by passing in the team identifier, rather than passing in a nested team object in my JSON object.

For example, I would like to be able to call POST / employee / with JSON, which looks like this:

{
    "team_id":"1",
    "name":"Fred Bloggs",
    "phone":"1234567890",
    "email":"test@example.com"
}

But instead, I need to pass something like this:

{
    "team":{
        "id":"1",
        }
    "name":"Fred Bloggs",
    "phone":"1234567890",
    "email":"test@example.com"
}

So my question is: how do others handle linking in JSON / REST without going around whole graphs of objects?

Sorry, this is such a fragmentary question, but, as I said, I'm just starting and terminology is a problem for me at this stage!

+3
source share
4 answers

, { "id":"1" }, , !

, , sub-JSONObject , , "1" . HATEOAS, .

POST:

{
    "team_href" : "/teams/1",
    "name":"can'tbebothered"
}

, , # 1 , () URI.

+1

, xml-link, json:


{
  ...
  links: 
  [
      {
          "href" : "/teams/1",
          "rel" : "team"
      },
      {
          "href" : "/teams/2",
          "rel" : "team"
      }
  ]
}

, ( rel). HTTP REST, .

( ), . /employee/123?inline=true. , . - , ( XML, ).

+1

REST URL- , . , :

{
    "team":"http://myapp.com/team/1",
    "name":"Fred Bloggs",
    "phone":"1234567890",
    "email":"test@example.com"
}

,

{
    "team":"1",
    "name":"Fred Bloggs",
    "phone":"1234567890",
    "email":"test@example.com"
}

, , ( , , ), JSON, .

0

. RESTful Web Services. , , , - JAXB , JAXB ( XML JSON) HATEOAS.

HATEOAS , RESTful WS - Atom Syndication JSON. .

GET /team/ - returns a paginated Atom Syndication Feed list of teams
POST /team/ - creates a new team receiving a JSON representation
GET /team/1 - returns a paginated Atom Syndication Feed list of employees in the 
              team with the ID 1 with an Link to team JSON representation too.

GET /employee/ - returns a paginated Atom Syndication Feed list of employees
POST /employee/ - creates a new employee using JSON
GET /employee/1 - returns details about the employee with the ID 1 in JSON

, . / .

@Path("/team/{id}/employees)
class TeamEmployees {

  @Path("/{posId}")
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Employee get(@PathParam("posId") int positonId) {}

  @Path("/{posId}")
  @DELETE
  public Employee remove(@PathParam("posId") int positonId) {}

  @POST
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  //empUri sample is /employee/{id} server knows how to parse it
  public Employee add(@FormParam("employeeUri") String empUri) {}

}

, , - , .. , position_id, team_id, emp_id . , 2 2 position_id. , HATEOAS.

, URI JSON DTO, , , DTO, , , (de) DTO, . API DTO SPI RESTful WS.

0

Source: https://habr.com/ru/post/1760815/


All Articles