软件测试学习笔记丨 Spring Boot 统一返回结果模版
作者:测试人
- 2024-05-15 北京
本文字数:1962 字
阅读完需:约 6 分钟
本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/30462
一, 返回结果模版示例
{
"success": true,
"code": 0,
"msg": "成功",
"data": {
"user_remedys": [
{
"user_id": "abd754f7",
"work_type": 1,
"status": 2,
"reason": "忘记打卡",
}
]
}
复制代码
二, 实现
2.1 定义一个 Result 模板类
package com.ceshiren.springtest.util;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
@ApiModel(value="Result返回模板类",description="返回结果体封装")
public class Result<T> implements Serializable {
//HashMap已实现的序列化实例对象
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "是否成功", example = "true")
private boolean success;
@ApiModelProperty(value = "返回结果码 0 成功 1 失败", example = "1", required = true, allowableValues = "1,0")
private int code;
@ApiModelProperty(value = "返回消息", example = "成功", allowableValues = "成功,失败" )
private String message;
@ApiModelProperty(value = "响应结果数据")
private T data;
public static <T> Result<T> OK(){
Result<T> result = new Result<>();
result.setSuccess(true);
result.setCode(ResultCode.SUCCESS);
result.setMessage("成功");
return result;
}
public static <T> Result<T> error(){
Result<T> result = new Result<>();
result.setSuccess(false);
result.setCode(ResultCode.ERROR);
result.setMessage("失败");
return result;
}
public Result<T> message(String message){
this.setMessage(message);
return this;
}
public Result<T> code(Integer code){
this.setCode(code);
return this;
}
public Result<T> data(T value){
this.setData(value);
return this;
}
}
复制代码
2.2 新建一个接口类 User2Service,定义一个登录接口
package com.ceshiren.springtest.service;
import com.ceshiren.springtest.dto.UserDto;
import com.ceshiren.springtest.util.Result;
public interface User2Service {
//用户登录接口
Result login(UserDto userDto);
}
复制代码
2.3 创建一个接口实现类 User2ServiceImpl
package com.ceshiren.springtest.service;
import com.ceshiren.springtest.dto.UserDto;
import com.ceshiren.springtest.util.Result;
import org.springframework.stereotype.Service;
@Service("user2")
public class User2ServiceImpl implements User2Service{
@Override
public Result login(UserDto userDto) {
if(userDto.getUsername().equals("demi") && userDto.getPassword().equals("123456")){
// return Result.OK().data(userDto);
return Result.OK().data(userDto).code(1000).message("登录成功");
}else{
// return Result.error().data(userDto);
return Result.error().data(userDto).code(1003).message("登录失败");
}
}
}
复制代码
2.4 创建一个控制类 UserController
package com.ceshiren.springtest.controller;
import com.ceshiren.springtest.dto.UserDto;
import com.ceshiren.springtest.service.User2Service;
import com.ceshiren.springtest.util.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api("user登录接口")
public class UserController {
@Autowired
User2Service user2;
@PostMapping(value = "/user/login", produces = "application/json")
Result logIn(@RequestBody UserDto userDto){
return user2.login(userDto);
}
}
复制代码
2.5 Swagger 页面展示
软件测试开发免费视频教程分享
划线
评论
复制
发布于: 刚刚阅读数: 4
版权声明: 本文为 InfoQ 作者【测试人】的原创文章。
原文链接:【http://xie.infoq.cn/article/fcd5971bad2e36d9c05cc1873】。文章转载请联系作者。
测试人
关注
专注于软件测试开发 2022-08-29 加入
霍格沃兹测试开发学社,测试人社区:https://ceshiren.com/t/topic/22284
评论