建立一个多模块的项目结构
我们一个项目一般都是由多个模块组成的,用来构建我们的微服务项目
1.建立sb项目
2.保留文件如下图
3.建立模块,项目格式如下图
修改parent项目的pom.xml文件为一下格式
修改打包格式、子项目模块的引用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 父模块打包类型必须为pom -->
<packaging>pom</packaging>
<name>demo1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>21</java.version>
</properties>
<modules>
<module>web</module>
<module>api</module>
<module>common</module>
<module>dao</module>
<module>service</module>
</modules>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
使用maven很慢可使用阿里源
<repositories>
<repository>
<id>alimaven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
父模块和子模块之间的继承
父模块
<!-- 父模块的pom.xml -->
<project>
<groupId>com.example</groupId>
<artifactId>parent-module</artifactId>
<version>1.0.0</version>
<dependencies>
<!-- 父模块的依赖 -->
<dependency>
<!-- ... -->
</dependency>
</dependencies>
</project>
子模块继承父模块,同时也会继承父模块的依赖
<!-- 子模块的pom.xml -->
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>parent-module</artifactId>
<version>1.0.0</version>
</parent>
<!-- 子模块的依赖会继承父模块的依赖 -->
</project>
ing 注意
如果模块之间引用,比如A模块引用B模块,那么B模块的pom.xml配置文件会被A引用,
子模块的配置信息会继承父模块的 它会自动继承父模块的依赖信息,如果子模块的配置信息包含了父模块的配置,那么父模块中相同依赖的会被子模块覆盖
引入Swagger
Swagger 是一个开源框架,用于设计、构建、文档化和消费 RESTful Web 服务。它提供了一套工具和约定,使得开发人员能够更轻松地设计和使用 REST API。
首先pom.xml引入
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
配置一个Swagger路径
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.web"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("Swagger Test Api")
.description("Restful Api测试")
.version("1.0")
.build();
}
}
配置swagger接口的介绍描述
@Api 注解标注在类上用来描述整个 Controller 信息。
@ApiOperation 注解标注在方法上,用来描述一个方法的基本信息。
@ApiImplicitParam 注解标注在方法上,用来描述方法的参数。其中 paramType 是指方法参数的类型,有如下可选值:
path:参数获取方式为 @PathVariable,这个是从路径获取参数
query:参数获取方式为 @RequestParam
header:参数获取方式为 @RequestHeader
(4)如果有多个参数,可以将多个参数的 @ApiImplicitParam 注解放到 @ApiImplicitParams 中。
(5)@ApiResponse 是对响应结果的描述。code 表示响应码,message 为相应的描述信息。如果有多个 @ApiResponse,则放在一个 @ApiResponses 中。
(6)@ApiIgnore 注解表示不对某个接口生成文档。
@RestController
@Api("用户接口")
public class Controller {
@Autowired
private UserServiceImpl userService;
@ApiOperation(value = "查询用户",notes = "根据用户名查询用户")
@GetMapping("/test/{username}")
public UserDto test(@PathVariable String username){
return userService.GetUser(username);
}
@GetMapping("/add/{username}")
public String test2(@PathVariable String username){
userService.AddUser(new UserDto(username,"xx"));
return "tes2";
}
}