1.1SpringBoot开发WebAPI

使用Spring Boot开发Web API

Maven功能特性和最佳实践

https://docs.qq.com/mind/DRU5LdHZlU01SZkZr?subId=BB08J2&mode=mind

Maven的作用

  • 自动化构建工具
    • 完整的构建生命周期
    • 标准化构建过程
  • 其他
    • 依赖管理
    • 仓库(中央仓库)

坐标与依赖

依赖管理

最佳实践

建立专用的依赖管理工程

该工程中只有一个pom文件

灵活使用属性变量

多环境配置

其他

同一工程下项目引入参数

1
2
3
4
5
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ioc-container-overview</artifactId>
<version>${project.version}</version>
</dependency>

代码工程1.0

约定

开发

Lombok

1
2
3
4
5
6
7
8
9
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class MyTest {
String name;
String addr;
}

AOP+日志框架

slf4j MDC

Controller技术组件

1
2
3
4
5
6
7
8
9
@RestController
@RequestMapping("/customerStaffs")
public class CustomerStaffController {
@PostMapping("/")
public Result<Long> addCustomerStaff(@RequestBody AddCustomerStaffReqVO addCustomerStaffReqVO) {...}

@GetMapping("/{staffId}")
public Result<CustomerStaffRespVO> findCustomerStaffById(@PathVariable("staffId") Long staffId) {...}
}

mapstruct

1
2
3
4
5
6
7
8
9
10
11
12
13
@Mapper
public interface CustomerStaffConverter {

CustomerStaffConverter INSTANCE = Mappers.getMapper(CustomerStaffConverter.class);

//VO->Entity
CustomerStaff convertCreateReq(AddCustomerStaffReqVO addCustomerStaffReqVO);
CustomerStaff convertUpdateReq(UpdateCustomerStaffReqVO updateCustomerStaffReqVO);

//Entity->VO
CustomerStaffRespVO convertResp(CustomerStaff entity);
List<CustomerStaffRespVO> convertListResp(List<CustomerStaff> entities);
}

问题

  • 多级代码工程中,如何有效管理对第三方组件的依赖关系?
    建立独立的依赖工程,利用maven提供的手段依赖排除、归类、范围进行管理