在进行接口开发时,一般需要一个固定的返回样式,成功和失败的时候,都按照这种格式来进行统一的返回,这样,在与其他人进行接口之间的联调时不会显得很杂乱无章,并且前端方便对返回结果统一处理。而这种固定的格式如果放在 Java 的每个接口单独处理时,又会在接口开发时很繁琐,所以这个时候可以采用封装一个实体类,统一返回固定模板格式的内容。
现在对我们之前写的登录接口返回进行统一封装处理。
新建 common 包,并且在 common 包里新建 StatusCode.java,这个类主要是统一定义程序里用到的错误码
package com.syy.testPlatform.common;
public class StatusCode {
//请登录
public static final Integer NEED_LOGIN = 401;
/** 操作成功*/
public static final Integer OPERATION_SUCCESS = 200;
/** 用户名/密码错误*/
public static final Integer USER_NAME_OR_PASSWORD_ERROR = 1011;
/** 用户不存在*/
public static final Integer USER_NOT_EXIST = 1012;
/** 用户名不能为空*/
public static final Integer USER_NAME_NULL = 1013;
/** 密码不能为空*/
public static final Integer USER_PASS_NULL = 1014;
}
在 common 包里新建 ResultType.java,这个类是用来定义相应错误码对应的话术
package com.syy.testPlatform.common;
public class ResultType {
public static final String NEED_LOGIN = "温馨提示:请使用对应的账号登录";
public static final String LOGIN_SUCCESS = "登录成功";
public static final String USER_NAME_OR_PASSWORD_ERROR = "用户名/密码错误";
public static final String USER_NOT_EXIST = "用户不存在";
public static final String USER_NAME_NULL = "用户名不能为空";
public static final String USER_PASS_NULL = "密码不能为空";
}
在 bean 包新建 Page.java 和 Result.java,其中 page 是用来返回分页信息的,Result 是统一返回结果
package com.syy.testPlatform.bean;
import lombok.Data;
import org.springframework.stereotype.Component;
@Data
@Component
public class Page {
private static final long serialVersionUID = 1L;
private int pageNum;
private int pageSize;
private int startRow;
private int endRow;
private long total;
private int pages;
private boolean count;
private Boolean reasonable;
private Boolean pageSizeZero;
private String countColumn;
private String orderBy;
private boolean orderByOnly;
}
package com.syy.testPlatform.bean;
import com.syy.testPlatform.common.StatusCode;
import lombok.Data;
import java.io.Serializable;
@Data
public class Result implements Serializable {
/** 成功失败 */
private boolean success;
/** 状态码 */
private Integer code;
/** 返回结果集 */
private Object data;
/** 信息 */
private String message;
/**分页信息 */
private Page pageInfo;
/** 前端弹窗模式:"warning", "error", "success", "info" */
private String icon;
public Result() {}
public Result(boolean success, String message) {
this.success = success;
this.message = message;
if (success) icon = "success";
else icon = "error";
}
public Result(boolean success, Integer code, String message) {
this(success, message);
this.code = code;
}
public Result(boolean success, Integer code, Object data, String message) {
this(success, code, message);
this.data = data;
}
public static Result result(Object data, boolean success, Integer code, String message) {
Result result = new Result();
result.data = data;
result.success = success;
result.message = message;
result.code = code;
return result;
}
public static Result result(Object data, Page pageInfo, boolean success, Integer code, String message) {
Result result = new Result();
result.pageInfo = pageInfo;
result.data = data;
result.success = success;
result.message = message;
result.code = code;
return result;
}
//返回成功结果
public static Result resultSuccess(Page pageInfo, Object data, String message) {
Result result = new Result();
result.success = true;
result.code = StatusCode.OPERATION_SUCCESS;
result.message = message;
result.pageInfo = pageInfo;
result.data = data;
return result;
}
//返回失败
public static Result resultError(String message, int code) {
Result result = new Result();
result.success = false;
result.message = message;
result.code = code;
return result;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public Page getPageInfo() {
return pageInfo;
}
public void setPageInfo(Page pageInfo) {
this.pageInfo = pageInfo;
}
}
对 UserController.java 和 UserServiceImpl.java 中的 login 方法进行统一返回的修改
package com.syy.testPlatform.controller;
import com.syy.testPlatform.bean.Result;
import com.syy.testPlatform.bean.User;
import com.syy.testPlatform.common.ResultType;
import com.syy.testPlatform.common.StatusCode;
import com.syy.testPlatform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/v1/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/queryUser")
public List<User> queryUser(){
List<User> userList = userService.queryUserList();
return userList;
}
@PostMapping("/login")
public Result login(@RequestBody User user){
if (user == null){
return Result.resultError(ResultType.USER_NAME_NULL, StatusCode.USER_NAME_NULL);
}
if (user.getUserName() == null || "".equals(user.getUserName())){
return Result.resultError(ResultType.USER_NAME_NULL, StatusCode.USER_NAME_NULL);
}
if (user.getPassword() == null || "".equals(user.getPassword())){
return Result.resultError(ResultType.USER_PASS_NULL, StatusCode.USER_PASS_NULL);
}
return userService.login(user.getUserName(), user.getPassword());
}
}
package com.syy.testPlatform.service.impl;
import com.syy.testPlatform.bean.Result;
import com.syy.testPlatform.bean.User;
import com.syy.testPlatform.common.ResultType;
import com.syy.testPlatform.common.StatusCode;
import com.syy.testPlatform.dao.UserDao;
import com.syy.testPlatform.service.UserService;
import com.syy.testPlatform.util.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> queryUserList() {
return userDao.queryUserList();
}
@Override
public User queryUserByName(String userName) {
return userDao.queryUserByName(userName);
}
@Override
public Result login(String userName, String password) {
User user = queryUserByName(userName);
if (user == null){
return Result.resultError(ResultType.USER_NOT_EXIST, StatusCode.USER_NOT_EXIST);
}
String passwordParam = Md5Utils.string2MD5(password);
if (!passwordParam.equals(user.getPassword())){
return Result.resultError(ResultType.USER_NAME_OR_PASSWORD_ERROR, StatusCode.USER_NAME_OR_PASSWORD_ERROR);
}
return Result.resultSuccess(null,null,ResultType.LOGIN_SUCCESS);
}
}