Java Spring Boot Rest项目推荐的项目结构是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40902280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:58:31  来源:igfitidea点击:

What is the recommended project structure for spring boot rest projects?

javaspringrestspring-boot

提问by niccolo m.

I'm a beginner with spring boot. I'm involved in the beginning of a project where we would build rest services using spring boot. Could you please advise the recommended directory structure to follow when building a project that will just expose rest services?

我是弹簧靴的初学者。我参与了一个项目的开始,我们将使用 Spring Boot 构建休息服务。您能否建议在构建仅公开休息服务的项目时遵循的推荐目录结构?

采纳答案by The Mighty Programmer

You do notneed to do anything special to start. Start with a normal java project, either maven or gradle or IDE project layout with starter dependency.

不会需要做什么特别的开始。从一个普通的 java 项目开始,maven 或 gradle 或具有 starter 依赖关系的 IDE 项目布局。

You need just one main class, as per guide hereand rest...

根据此处的指南,您只需要一个主要课程,然后休息......

There is no constrained package structure. Actual structure will be driven by your requirement/whim and the directory structure is laid by build-tool / IDE

没有受约束的包结构。实际结构将由您的需求/心血来潮驱动,目录结构由 build-tool / IDE 奠定

You can follow same structure that you might be following for a Spring MVC application.

您可以遵循与 Spring MVC 应用程序相同的结构。

You can follow either way

你可以按照任何一种方式

  • A project is divided into layers:

    for example: DDD style

    • Service layer : service package contains service classes
    • DAO/REPO layer : dao package containing dao classes
    • Entity layers


    or

    any layer structure suitable to your problem for which you are writing problem.

  • A project divided into modules or functionalities or features and A module is divided into layers like above

  • 一个项目分为几个层次:

    例如:DDD风格

    • 服务层:服务包包含服务类
    • DAO/REPO层:包含dao类的dao包
    • 实体层


    或者

    任何适合您正在编写问题的问题的层结构。

  • 一个项目分为模块或功能或特性,一个模块像上面一样分为多个层

I prefer the second, because it follows Business context. Think in terms of concepts.

我更喜欢第二种,因为它遵循商业环境。从概念上思考。

What you do is dependent upon how you see the project. It is your code organization skills.

你做什么取决于你如何看待这个项目。这是你的代码组织技能。

回答by JoB?N

Please use Spring Tool Suite (Eclipse-based development environment that is customized for developing Spring applications).
Create a Spring Starter Project, it will create the directory structure for you with the spring boot maven dependencies.

请使用 Spring Tool Suite(为开发 Spring 应用程序而定制的基于 Eclipse 的开发环境)。
创建一个 Spring Starter 项目,它将为您创建带有 spring boot maven 依赖项的目录结构。

回答by Rahul Kumar

Use Link-1 to generate a project. this a basic project for learning. you can understand the folder structure. Use Link-2 for creating a basic Spring boot project. 1: http://start.spring.io/2: https://projects.spring.io/spring-boot/

使用 Link-1 生成项目。这是一个基本的学习项目。您可以了解文件夹结构。使用 Link-2 创建一个基本的 Spring boot 项目。1:http: //start.spring.io/2:https: //projects.spring.io/spring-boot/

Create a gradle/maven project Automatically src/main/java and src/main/test will be created. create controller/service/Repository package and start writing the code.

创建 gradle/maven 项目会自动创建 src/main/java 和 src/main/test 。创建控制器/服务/存储库包并开始编写代码。

-src/main/java(source folder) ---com.package.service(package) ---ServiceClass(Class) ---com.package.controller(package) ---ControllerClass(Class)

-src/main/java(源文件夹) ---com.package.service(package) ---ServiceClass(Class) ---com.package.controller(package) ---ControllerClass(Class)

回答by Alireza Fattahi

There is a somehowrecommended directory structure mentioned at https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html提到了某种推荐的目录结构

You can create a apifolder and put your controllers there.

您可以创建一个api文件夹并将您的控制器放在那里。

If you have some configuration beans, put them in a separate package too.

如果您有一些配置 bean,也将它们放在一个单独的包中。

回答by Nghia Do

I have an example which I have been using for couple years. Please take a look as a reference.

我有一个我已经使用了几年的例子。请看一下作为参考。

https://github.com/bigzidane/springboot-rest-h2-swagger

https://github.com/bigzidane/springboot-rest-h2-swagger

回答by Subhasish Sahu

config - class which will read from property files

config - 将从属性文件中读取的类

cache - caching mechanism class files

cache - 缓存机制类文件

constants - constant defined class

常量 - 常量定义类

controller - controller class

控制器 - 控制器类

exception - exception class

异常 - 异常类

model - pojos classes will be present

模型 - 将出现 pojos 类

security - security classes

安全 - 安全类

service - Impl classes

服务 - 实现类

util - utility classes

util - 实用程序类

validation - validators classes

验证 - 验证器类

bootloader - main class

引导加载程序 - 主类

回答by Amar Prakash Pandey

Though this question has an accepted answer, still I would like to share my project structure for RESTful services.

虽然这个问题有一个公认的答案,但我仍然想分享我的 RESTful 服务项目结构。

src/main/java
    +- com
        +- example
            +- Application.java
            +- ApplicationConstants.java
                +- configuration
                |   +- ApplicationConfiguration.java
                +- controller
                |   +- ApplicationController.java
                +- dao
                |   +- impl
                |   |   +- ApplicationDaoImpl.java
                |   +- ApplicationDao.java
                +- dto
                |   +- ApplicationDto.java
                +- service
                |   +- impl
                |   |   +- ApplicationServiceImpl.java
                |   +- ApplicationService.java
                +- util
                |   +- ApplicationUtils.java
                +- validation
                |   +- impl
                |   |   +- ApplicationValidationImpl.java
                |   +- ApplicationValidation.java

DAO= Data Access Object.
DTO= Data Transfer Object.

DAO= 数据访问对象。
DTO= 数据传输对象。

回答by ENDEESA

From the docs:, this is the recommended way

文档:,这是推荐的方式

enter image description here

在此处输入图片说明

回答by Md. Tahmid Mozaffar

https://medium.com/the-resonant-web/spring-boot-2-0-project-structure-and-best-practices-part-2-7137bdcba7d3

https://medium.com/the-resonant-web/spring-boot-2-0-project-structure-and-best-practices-part-2-7137bdcba7d3

I think this is a good structure. And it is a nicely written blog explaining the mindset of these choices.

我认为这是一个很好的结构。这是一篇写得很好的博客,解释了这些选择的心态。