Matthijs Mennens
Matthijs Mennens

Reputation: 1145

REST service project structure

I was working on a project with a colleague and he told me that I should create 3 levels when creating a rest service. Can anyone explain me if this is the 'normal' and if I use it in the correct way? He never explained me why and left the firm a few weeks later.

First level: Resource

I imagine this is where you catch the requests (GET, POST, PUT, etc.)

Second level: Service

I would make sense that calculations happen here?

Third level: Repository

This is where he put all the statements that connect with the Database.

e.g. Say we have an entity 'Employee' with a property 'level'. Depending on your level, you get a different bonus. We want to calculate the bonus and return it via a REST service.

The resource method would look something like this:

@GET
@Path("/id/{id}")
@Produces(MediaType.APPLICATION_JSON)
public double findBonusById(@PathParam("id") long id) {
    return employeeService.findBonusById(id);
}

The service method would look something like this:

public double findBonusById(long id) {
    int level = employeeRepository.findLevelById(id);
    double initialBonus = 2563;
    return level * initialBonus;
}

The repository method would look something like this:

public int findLevelById(long id) {
    return getEntityManager().createNamedQuery("Employee.findLevelById").setParameter("id", id).getSingleResult();
}

Upvotes: 1

Views: 157

Answers (2)

Yogendra Mishra
Yogendra Mishra

Reputation: 2609

Yes Matthijs, that's normal. While building any webservice related projects what people prefer is different "layer" of separation. i.e layer architecture

  1. Controller :- where your request lands.

    @GET @Path("/id/{id}") @Produces(MediaType.APPLICATION_JSON) public double findBonusById(@PathParam("id") long id) { return employeeService.findBonusById(id); }

  2. Service :- where you perform business logic

    public double findBonusById(long id) { int level = employeeRepository.findLevelById(id); double initialBonus = 2563; return level * initialBonus; }

  3. Repository :- where you communicate to your database.

    public int findLevelById(long id) { return getEntityManager().createNamedQuery("Employee.findLevelById").setParameter("id", id).getSingleResult(); }

These are some of the standard which normally people follow however there can be more layer of separation.

Upvotes: 2

Ryuzaki L
Ryuzaki L

Reputation: 40058

So this is called layered architecture, in your case

Controller which handles the request

@GET
@Path("/id/{id}")
@Produces(MediaType.APPLICATION_JSON)
public double findBonusById(@PathParam("id") long id) {
return employeeService.findBonusById(id);
}

Service is a layer which communicates between controller and Repository

public double findBonusById(long id) {
int level = employeeRepository.findLevelById(id);
double initialBonus = 2563;
return level * initialBonus;
}

Business This is optional layer and depends on requirement, you can have all your business logic in this layer

double initialBonus = 2563;
return level * initialBonus;

Repository this is as you said This is where he put all the statements that connect with the Database.

public int findLevelById(long id) {
return getEntityManager().createNamedQuery("Employee.findLevelById").setParameter("id", id).getSingleResult();
 }

Upvotes: 4

Related Questions