花10分钟写个漂亮的后端API接口模板!
ztj100 2025-05-11 19:43 4 浏览 0 评论
在这微服务架构盛行的黄金时段,加上越来越多的前后端分离,导致后端API接口规范变得越来越重要了。
比如:统一返回参数形式、统一返回码、统一异常处理、集成swagger等。
目的主要是规范后端项目代码,以及便于前端沟通联通以及问题的排查。
本文内容:
统一返回参数形式
目前主流的返回参数形式:
{
"code": 200000,
"message": "成功",
"data": {
"id": 1,
"userName": "tiange",
"password": "123456",
"phone": "18257160375",
"gender": 0,
"status": 0,
"createTime": "2024-05-17 20:24:40"
}
}
code是接口返回编码,message是消息提示,data是具体返回数据内容。
返回码
返回码定义很重要,我们应该可以参考HTTP请求返回的状态码(下面是常见的HTTP状态码):
200 - 请求成功
301 - 资源(网页等)被永久转移到其它URL
404 - 请求的资源(网页等)不存在
500 - 内部服务器错误
这样前端开发人员在得到返回值后,根据状态码就可以知道,大概什么错误,再根据message相关的信息描述,可以快速定位。
由于我们业务系统中可能会又大量的code,所以,我们对此做一个改良。
/**
* {@code @description:} 返回码
*
* @author tianwc
*
* {@code @date:} 2024-07-28 15:10
* {@code @version:} 1.0
*/
@Getter
public enum ResultCode implements Serializable {
SUCCESS(200000, "成功"),
FAIL(500000, "系统错误,请稍后重试!"),
USER_NOT_EXIST(401000, "用户不存在"),
USER_CANCELLED(401001, "用户已注销"),
USER_ROLE_ERROR(401002, "用户角色不对"),
NOT_FOUND(404000, "接口不存在"),
PARAMETER_ERROR(404001, "参数有误"),
PARAMETER_IS_NULL(404002, "参数为空");
private final int code;
private final String message;
ResultCode(int code, String message) {
this.code = code;
this.message = message;
}
}
对此,我们还可以进一步细分,比如402开头的是用户相关的 、403开头又是xxx的,.....
这样后期如果又什么问题,这样就能快速定位到具体模块中。
统一返回
我们可以专门写一个类来对返回数据进行包装。
/**
* {@code @description:} 返回结果马甲
*
* @author tianwc
*
* {@code @date:} 2024-07-28 15:12
* {@code @version:} 1.0
*/
@Data
public class Result implements Serializable {
private Integer code;
private String message;
private Object data;
public Result(Integer code, String message, Object data) {
this.code = code;
this.message = message;
this.data = data;
}
public static Result success() {
return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
}
public static Result success(Object data) {
return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
}
public static Result fail(ResultCode resultCode) {
return new Result(resultCode.getCode(), resultCode.getMessage(), null);
}
public static Result fail(int code, String message) {
return new Result(code, message, null);
}
}
我们定义了常用的四种格式。
具体使用如下:
我们在Service层和实现层:
public interface UserInfoService extends IService<UserInfo> {
Result findByCondition(UserInfoReqDto userInfoReqDto);
}
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
@Override
public Result findByCondition(UserInfoReqDto userInfoReqDto) {
Wrapper<UserInfo> wrapper = Wrappers.<UserInfo>lambdaQuery()
.eq(UserInfo::getUserName, userInfoReqDto.getUserName())
.eq(UserInfo::getPassword, userInfoReqDto.getPassword());
return Result.success(this.baseMapper.selectList(wrapper));
}
}
在controller层:我们会在controller层处理业务请求,并返回给前端 。
@RestController
@RequestMapping("/user/info")
public class UserInfoController {
@Resource
private UserInfoService userInfoService;
@GetMapping("/condition")
public Result findByCondition(UserInfoReqDto userInfoReqDto) {
return userInfoService.findByCondition(userInfoReqDto);
}
}
执行:
GET http://localhost:8089/user/info/condition?userName=tiange&password=123456
返回:
{
"code": 200000,
"message": "成功",
"data": [
{
"id": 1,
"userName": "tiange",
"password": "123456",
"phone": "18257160375",
"gender": 0,
"status": 0,
"createTime": "2024-05-17T20:24:40.000+00:00"
}
]
}
前端根据我们但会的code判断是否需要取data字段。
统一异常处理
统一异常处理我们分业务异常、系统异常以及参数异常:
业务异常
我们自定义一个业务异常:BusinessException
/**
* @author tianwc
* @version 1.0.0
* @date 2024-07-28 15:12
*
* <p>
* 自定义业务异常
*/
@Getter
public class BusinessException extends RuntimeException {
/**
* http状态码
*/
private Integer code;
private Object object;
public BusinessException(String message, Integer code, Object object) {
super(message);
this.code = code;
this.object = object;
}
public BusinessException(String message, Integer code) {
super(message);
this.code = code;
}
public BusinessException(ResultCode resultCode) {
super(resultCode.getMessage());
this.code = resultCode.getCode();
this.object = resultCode.getMessage();
}
public BusinessException(ResultCode resultCode, String message) {
this.code = resultCode.getCode();
this.object = message;
}
public BusinessException(String message) {
super(message);
}
}
异常处理:GlobalAdvice
@RestControllerAdvice
@Slf4j
public class GlobalAdvice {
@ExceptionHandler(Exception.class)
public Result doException(Exception e) {
log.error("统一异常处理机制,触发异常 msg ", e);
String message = null;
int errorCode = ResultCode.FAIL.getCode();
//自定义异常
if (e instanceof BusinessException) {
BusinessException exception = (BusinessException) e;
message = exception.getMessage();
errorCode = exception.getCode();
} else if (e instanceof HttpRequestMethodNotSupportedException) {
message = "不支持GET/POST方法";
} else if (e instanceof NoHandlerFoundException) {
message = "请求接口不存在";
} else if (e instanceof MissingServletRequestParameterException) {
errorCode = ResultCode.PARAMETER_IS_NULL.getCode();
message = String.format("缺少必要参数[%s]", ((MissingServletRequestParameterException) e).getParameterName());
} else if (e instanceof MethodArgumentNotValidException) {
BindingResult result = ((MethodArgumentNotValidException) e).getBindingResult();
FieldError error = result.getFieldError();
errorCode = ResultCode.PARAMETER_IS_NULL.getCode();
message = error == null ? ResultCode.PARAMETER_ERROR.getMessage() : error.getDefaultMessage();
} else if (e instanceof BindException) {
errorCode = ResultCode.PARAMETER_IS_NULL.getCode();
message = ((BindException) e).getFieldError().getDefaultMessage();
} else if (e instanceof IllegalArgumentException) {
errorCode = ResultCode.PARAMETER_IS_NULL.getCode();
message = e.getMessage();
}
return Result.fail(errorCode, message);
}
}
使用:
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
@Override
public Result findByCondition(UserInfoReqDto userInfoReqDto) {
if("admin".equals(userInfoReqDto.getUserName())){
//对于某些业务问题抛出自定义异常
throw new BusinessException(ResultCode.USER_ROLE_ERROR);
}
Wrapper<UserInfo> wrapper = Wrappers.<UserInfo>lambdaQuery()
.eq(UserInfo::getUserName, userInfoReqDto.getUserName())
.eq(UserInfo::getPassword, userInfoReqDto.getPassword());
return Result.success(this.baseMapper.selectList(wrapper));
}
}
系统异常
假设系统异常:
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
@Override
public Result findByCondition(UserInfoReqDto userInfoReqDto) {
if("123456".equals(userInfoReqDto.getPassword())){
throw new RuntimeException("你的系统异常了");
}
Wrapper<UserInfo> wrapper = Wrappers.<UserInfo>lambdaQuery()
.eq(UserInfo::getUserName, userInfoReqDto.getUserName())
.eq(UserInfo::getPassword, userInfoReqDto.getPassword());
return Result.success(this.baseMapper.selectList(wrapper));
}
}
执行:
GET http://localhost:8089/user/info/condition?userName=tian&password=123456
返回结果:
{
"code": 500000,
"message": "系统异常",
"data": null
}
参数校验
添加pom依赖
<!--参数验证-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
请求参数:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfoReqDto {
@NotBlank(message = "姓名不能为空")
private String userName;
@NotBlank(message = "密码不能为空")
private String password;
}
其他相关注解:
注解作用@NotNull判断包装类是否为null@NotBlank判断字符串是否为null或者是空串(去掉首尾空格)@NotEmpty判断集合是否为空@Length判断字符的长度(最大或者最小)@Min判断数值最小值@Max判断数值最大值@Email判断邮箱是否合法
controller层添加注解@Validated
@RestController
@RequestMapping("/user/info")
public class UserInfoController {
@Resource
private UserInfoService userInfoService;
@GetMapping("/condition")
public Result findByCondition(@Validated UserInfoReqDto userInfoReqDto) {
return userInfoService.findByCondition(userInfoReqDto);
}
}
最后在统一异常处理里处理。
执行:
GET http://localhost:8089/user/info/condition?userName=tian
返回:
{
"code": 404002,
"message": "密码不能为空",
"data": null
}
执行:
GET http://localhost:8089/user/info/condition?password=123456
返回:
{
"code": 404002,
"message": "姓名不能为空",
"data": null
}
集成mybatis-plus
添加依赖
<!--mybatis-plus 依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
数据库信息配置:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/user-center?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456
mybatis-plus配置:
@Configuration
@MapperScan(basePackages = "com.tian.dao.mapper")
public class DataSourceConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//注册乐观锁插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisPlusInterceptor interceptor) throws Exception {
MybatisSqlSessionFactoryBean ssfb = new MybatisSqlSessionFactoryBean();
ssfb.setDataSource(dataSource);
ssfb.setPlugins(interceptor);
//到哪里找xml文件
ssfb.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/*.xml"));
return ssfb.getObject();
}
}
实体类:
@TableName(value = "user_info")
@Data
public class UserInfo {
/**
* 主键ID
*/
@TableId(value = "id")
private Long id;
/**
* 姓名
*/
@TableField(value = "user_name")
private String userName;
/**
* 密码
*/
@TableField(value = "password")
private String password;
/**
* 手机号
*/
@TableField(value = "phone")
private String phone;
/**
* 性别,0:女,1:男
*/
@TableField(value = "gender")
private Integer gender;
/**
* 状态,0:正常,1:已注销
*/
@TableField(value = "status")
private Integer status;
/**
* 注册时间
*/
@TableField(value = "create_time")
private Date createTime;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}
mapper:
public interface UserInfoMapper extends BaseMapper<UserInfo> {
}
service部分代码参照前面的代码来。
执行
GET http://localhost:8089/user/info/condition?userName=tiange&password=123456
返回
{
"code": 200000,
"message": "成功",
"data": [
{
"id": 1,
"userName": "tiange",
"password": "123456",
"phone": "18257160375",
"gender": 0,
"status": 0,
"createTime": "2024-05-17T20:24:40.000+00:00"
}
]
}
到这里我们的项目就成功把mybatis-plus集成进来。
swagger
作为前后端分离项目,在团队开发中,一个好的 API 文档不但可以减少大量的沟通成本,还可以帮助一位新人快速上手业务。传统的做法是由开发人员创建一份 RESTful API文档来记录所有的接口细节,并在程序员之间代代相传。这种做法存在以下几个问题:
1)API 接口众多,细节复杂,需要考虑不同的HTTP请求类型、HTTP头部信息、HTTP请求内容等,想要高质量的完成这份文档需要耗费大量的精力;
2)难以维护。随着需求的变更和项目的优化、推进,接口的细节在不断地演变,接口描述文档也需要同步修订,可是文档和代码处于两个不同的媒介,除非有严格的管理机制,否则很容易出现文档、接口不一致的情况;
Swagger2 的出现就是为了从根本上解决上述问题。它作为一个规范和完整的框架,可以用于生成、描述、调用和可视化 RESTful 风格的 Web 服务:
- 接口文档在线自动生成,文档随接口变动实时更新,节省维护成本;
- 支持在线接口测试,不依赖第三方工具;
Swagger2 是一个规范和完整的框架,用于生成、描述、调用和可视化Restful风格的web服务,现在我们使用spring boot 整合它。作用:
- 接口的文档在线自动生成;
- 功能测试;
常用注解
注解描述@Api将类标记为 Swagger 资源。@ApiImplicitParam表示 API 操作中的单个参数。@ApiImplicitParams允许多个 ApiImplicitParam 对象列表的包装器。@ApiModel提供有关 Swagger 模型的其他信息。@ApiModelProperty添加和操作模型属性的数据。@ApiOperation描述针对特定路径的操作或通常是 HTTP 方法。@ApiParam为操作参数添加额外的元数据。@ApiResponse描述操作的可能响应。@ApiResponses允许多个 ApiResponse 对象列表的包装器。@Authorization声明要在资源或操作上使用的授权方案。@AuthorizationScope描述 OAuth2 授权范围。
swagger配置
@Configuration //加入到容器里面
@EnableSwagger2 //开启Swagger
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.tian.controller"))
.build();
}
private ApiInfo apiInfo(){
Contact contact = new Contact("web项目demo", "https://www.woaijava.cc/", "251965157@qq.com");
return new ApiInfo(
"web项目demo的API文档",
"练手所用",
"v1.0",
"https://www.woaijava.cc/",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
我们就可以在对应业务代码中标注上swagger:
@RestController
@RequestMapping("/user/info")
@Api(value = "用户信息接口",tags = "用户信息")
public class UserInfoController {
@Resource
private UserInfoService userInfoService;
@GetMapping("/{id}")
@ApiOperation(value = "根据id查询用户信息", notes = "根据id查询用户信息"
,produces = "application/json",consumes = "application/json")
@ApiImplicitParams({
@ApiImplicitParam(name="id",value="用户id",required = true,dataType = "Integer")
})
public Result findById(@PathVariable("id") Integer id) {
return Result.success(userInfoService.getById(id));
}
@GetMapping("/condition")
@ApiOperation(value = "根据条件查询用户信息")
public Result findByCondition(@Validated UserInfoReqDto userInfoReqDto) {
return userInfoService.findByCondition(userInfoReqDto);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value="用户信息查询条件")
public class UserInfoReqDto {
@NotBlank(message = "姓名不能为空")
@ApiModelProperty(value="姓名")
private String userName;
@NotBlank(message = "密码不能为空")
@ApiModelProperty(value="密码")
private String password;
}
效果
启动项目,访问:
http://localhost:8089/swagger-ui.html
也到这里,我们就基本形成了一个完整的demo级后端项目。
- 上一篇:十年之重修MyBatis原理
- 下一篇:spring常用注解
相关推荐
-
- SpringBoot如何实现优雅的参数校验
-
平常业务中肯定少不了校验,如果我们把大量的校验代码夹杂到业务中,肯定是不优雅的,对于一些简单的校验,我们可以使用java为我们提供的api进行处理,同时对于一些...
-
2025-05-11 19:46 ztj100
- Java中的空指针怎么处理?
-
#暑期创作大赛#Java程序员工作中遇到最多的错误就是空指针异常,无论你多么细心,一不留神就从代码的某个地方冒出NullPointerException,令人头疼。...
- 一坨一坨 if/else 参数校验,被 SpringBoot 参数校验组件整干净了
-
来源:https://mp.weixin.qq.com/s/ZVOiT-_C3f-g7aj3760Q-g...
- 用了这两款插件,同事再也不说我代码写的烂了
-
同事:你的代码写的不行啊,不够规范啊。我:我写的代码怎么可能不规范,不要胡说。于是同事打开我的IDEA,安装了一个插件,然后执行了一下,规范不规范,看报告吧。这可怎么是好,这玩意竟然给我挑出来这么...
- SpringBoot中6种拦截器使用场景
-
SpringBoot中6种拦截器使用场景,下面是思维导图详细总结一、拦截器基础...
- 用注解进行参数校验,spring validation介绍、使用、实现原理分析
-
springvalidation是什么在平时的需求开发中,经常会有参数校验的需求,比如一个接收用户注册请求的接口,要校验用户传入的用户名不能为空、用户名长度不超过20个字符、传入的手机号是合法的手机...
- 快速上手:SpringBoot自定义请求参数校验
-
作者:UncleChen来源:http://unclechen.github.io/最近在工作中遇到写一些API,这些API的请求参数非常多,嵌套也非常复杂,如果参数的校验代码全部都手动去实现,写起来...
- 分布式微服务架构组件
-
1、服务发现-Nacos服务发现、配置管理、服务治理及管理,同类产品还有ZooKeeper、Eureka、Consulhttps://nacos.io/zh-cn/docs/what-is-nacos...
- 优雅的参数校验,告别冗余if-else
-
一、参数校验简介...
- Spring Boot断言深度指南:用断言机制为代码构筑健壮防线
-
在SpringBoot开发中,断言(Assert)如同代码的"体检医生",能在上线前精准捕捉业务逻辑漏洞。本文将结合企业级实践,解析如何通过断言机制实现代码自检、异常预警与性能优化三...
- 如何在项目中优雅的校验参数
-
本文看点前言验证数据是贯穿所有应用程序层(从表示层到持久层)的常见任务。通常在每一层实现相同的验证逻辑,这既费时又容易出错。为了避免重复这些验证,开发人员经常将验证逻辑直接捆绑到域模型中,将域类与验证...
- SpingBoot项目使用@Validated和@Valid参数校验
-
一、什么是参数校验?我们在后端开发中,经常遇到的一个问题就是入参校验。简单来说就是对一个方法入参的参数进行校验,看是否符合我们的要求。比如入参要求是一个金额,你前端没做限制,用户随便过来一个负数,或者...
- 28个验证注解,通过业务案例让你精通Java数据校验(收藏篇)
-
在现代软件开发中,数据验证是确保应用程序健壮性和可靠性的关键环节。JavaBeanValidation(JSR380)作为一个功能强大的规范,为我们提供了一套全面的注解工具集,这些注解能够帮...
- Springboot @NotBlank参数校验失效汇总
-
有时候明明一个微服务里的@Validated和@NotBlank用的好好的,但就是另一个里不能用,这时候问题是最不好排查的,下面列举了各种失效情况的汇总,供各位参考:1、版本问题springbo...
- 这可能是最全面的Spring面试八股文了
-
Spring是什么?Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- node卸载 (33)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- exceptionininitializererror (33)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)