一, 返回结果模版示例
{
"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页面展示