简体   繁体   English

REST服务项目结构

[英]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. 我正在与一位同事一起进行一个项目,他告诉我在创建休息服务时应该创建3个级别。 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.) 我想这是您捕获请求(GET,POST,PUT等)的地方

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. 他在这里放置了与数据库连接的所有语句。

eg Say we have an entity 'Employee' with a property 'level'. 例如,假设我们有一个实体“ Employee”,其属性为“ level”。 Depending on your level, you get a different bonus. 根据您的水平,您会获得不同的奖金。 We want to calculate the bonus and return it via a REST service. 我们要计算奖金并通过REST服务将其返还。

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();
}

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. 正如您所说的那样, This is where he put all the statements that connect with the Database. 存储库。这是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();
 }

Yes Matthijs, that's normal. 是的,Matthijs,这很正常。 While building any webservice related projects what people prefer is different "layer" of separation. 在构建任何与Web服务相关的项目时,人们喜欢的是不同的分离“层”。 ie 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. 这些是人们通常遵循的一些标准,但是可能会有更多的隔离层。

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

相关问题 REST 服务 PUT,xml 有效载荷结构 - REST Service PUT , xml payload structure 运行 Jersey 项目(休息 web 服务)到 tomcat - Running Jersey project (Rest web service) to tomcat 如何将Maven项目转换为Spring REST服务 - How to convert a Maven Project to Spring REST Service 用于在Java / Jersey REST项目(Maven)中保存图像文件的目录结构 - Directory Structure for saving image files in Java/Jersey REST project (Maven) 如何使用Java Rest服务在angular 2项目中捕获JavaScript事件 - How to catch javascript event in angular 2 project with a java rest service Jersey REST Web服务获取Web项目URL - Jersey REST web service get web project URL Intellij Rest服务项目-java.lang.ArrayIndexOutOfBoundsException - Intellij Rest Service Project - java.lang.ArrayIndexOutOfBoundsException Sip Servlet / REST服务项目中的Mobicent EJB注入 - Mobicent EJB Injection in a sip servlet/ rest service project 如何通过查看项目来确定哪种类型的 Web 服务(soap 或 rest) - How to determine which type of web service(soap or rest) by looking project 将spring-data-rest添加到gs-rest-service样本项目后,存在依赖性问题 - Dependency issue after adding spring-data-rest to gs-rest-service sample project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM