在前后端分離的情況下,我們經(jīng)常會(huì)定義一個(gè)統(tǒng)一的反回?cái)?shù)據(jù)格式,通常都會(huì)包含狀態(tài)碼,返回信息,返回的數(shù)據(jù),是否成功等參數(shù)。1、ResultCode 單獨(dú)定義了一個(gè)ReturnCode枚舉類用于存儲(chǔ)代碼和返回的M...
在前后端分離的情況下,我們經(jīng)常會(huì)定義一個(gè)統(tǒng)一的反回?cái)?shù)據(jù)格式,通常都會(huì)包含狀態(tài)碼,返回信息,返回的數(shù)據(jù),是否成功等參數(shù)。
1、ResultCode
單獨(dú)定義了一個(gè)ReturnCode枚舉類用于存儲(chǔ)代碼和返回的Message
public enum ResultCode {
//成功
SUCCESS(200),
// 失敗
FAIL(400),
// 未認(rèn)證(簽名錯(cuò)誤)
UNAUTHORIZED(401),
// 接口不存在
NOT_FOUND(404),
// 服務(wù)器內(nèi)部錯(cuò)誤
INTERNAL_SERVER_ERROR(500);
public int code;
ResultCode(int code)
{
this.code=code;
}
}
2、ResponseResult
/*
統(tǒng)一返回信息
*/
public class ResponseResult<T> {
public int code; //返回狀態(tài)碼200成功
private String msg; //返回描述信息
private T data; //返回內(nèi)容體
public ResponseResult<T> setCode(ResultCode retCode) {
this.code = retCode.code;
return this;
}
public int getCode() {
return code;
}
public ResponseResult<T> setCode(int code) {
this.code = code;
return this;
}
public String getMsg() {
return msg;
}
public ResponseResult<T> setMsg(String msg) {
this.msg = msg;
return this;
}
public T getData() {
return data;
}
public ResponseResult<T> setData(T data) {
this.data = data;
return this;
}
}
在定義一個(gè)統(tǒng)一返回類:
3、Response
public class Response {
private final static String SUCCESS = "success";
private final static String FAIL = "fail";
public static <T> ResponseResult<T> makeOKRsp() {
return new ResponseResult<T>().setCode(ResultCode.SUCCESS).setMsg(SUCCESS);
}
public static <T> ResponseResult<T> makeOKRsp(String message) {
return new ResponseResult<T>().setCode(ResultCode.SUCCESS).setMsg(message);
}
public static <T> ResponseResult<T> makeOKRsp(T data) {
return new ResponseResult<T>().setCode(ResultCode.SUCCESS).setMsg(SUCCESS).setData(data);
}
public static <T> ResponseResult<T> makeErrRsp(String message) {
return new ResponseResult<T>().setCode(ResultCode.INTERNAL_SERVER_ERROR).setMsg(message);
}
public static <T> ResponseResult<T> makeRsp(int code, String msg) {
return new ResponseResult<T>().setCode(code).setMsg(msg);
}
public static <T> ResponseResult<T> makeRsp(int code, String msg, T data) {
return new ResponseResult<T>().setCode(code).setMsg(msg).setData(data);
}
}
4、新建 IUserService
新建測(cè)試用戶接口類
package com.example.demo.service;
import com.example.demo.entity.User;
public interface IUserService {
public User getUserInfo();
}
5、新建 UserServiceImpl
新建測(cè)試用戶信息服務(wù)類
package com.example.demo.service.impl;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
@Service
public class UserServiceImpl implements IUserService {
public User getUserInfo(){
User user = new User();
user.setName("jack");
user.setPassword(12341234);
return user;
}
}
6、在controller調(diào)用
@Autowired
UserService service;
@RequestMapping(value = "/getUserItem",method = RequestMethod.GET)
public ResponseResult<User> getUserItem(){
try {
User user = service.getUserInfo();
String[] arr= new String[]{"測(cè)試"};
return Response.makeOKRsp(user);
}catch (Exception e)
{
return Response.makeErrRsp("查詢用戶信息異常");
}
}
返回結(jié)果:
7、全局異常處理器
/**
* 全局異常處理
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
/*============= 請(qǐng)求錯(cuò)誤 start ==============================================*/
/**
* HTTP 請(qǐng)求方式不支持異常
* HttpRequestMethodNotSupportedException
* @return {@link ResponseResult}
*/
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
public ResponseResult httpRequestMethodNotSupportException(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
return Response.makeErrRsp("請(qǐng)求方式不支持異常");
}
/*============= 請(qǐng)求錯(cuò)誤 end ==============================================*/
}
修改一下getUserItem讓其拋出自定義查詢返回null的異常:
————————————————
版權(quán)聲明:本文為CSDN博主「CurtainMy」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
來(lái)源:本文內(nèi)容搜集或轉(zhuǎn)自各大網(wǎng)絡(luò)平臺(tái),并已注明來(lái)源、出處,如果轉(zhuǎn)載侵犯您的版權(quán)或非授權(quán)發(fā)布,請(qǐng)聯(lián)系小編,我們會(huì)及時(shí)審核處理。
聲明:江蘇教育黃頁(yè)對(duì)文中觀點(diǎn)保持中立,對(duì)所包含內(nèi)容的準(zhǔn)確性、可靠性或者完整性不提供任何明示或暗示的保證,不對(duì)文章觀點(diǎn)負(fù)責(zé),僅作分享之用,文章版權(quán)及插圖屬于原作者。
Copyright©2013-2025 ?JSedu114 All Rights Reserved. 江蘇教育信息綜合發(fā)布查詢平臺(tái)保留所有權(quán)利
蘇公網(wǎng)安備32010402000125
蘇ICP備14051488號(hào)-3技術(shù)支持:南京博盛藍(lán)睿網(wǎng)絡(luò)科技有限公司
南京思必達(dá)教育科技有限公司版權(quán)所有 百度統(tǒng)計(jì)