1.返回一个异常为500

2.统一返回一个异常为201

统一异常处理
注意:common为项目中共用的代码!!
============1.1 HospitalSetController类==================
@ApiOperation(value = "根据id获取医院设置")
@GetMapping("getHospSEt/{id}")
public Result getHospitalSet(@PathVariable Long id){
int aa=2/0;
HospitalSet hospitalSet = hospitalSetService.getById(id);
return Result.ok(hospitalSet);
}
=======1.2测试======================
浏览器中访问http://localhost:8201/swagger-ui.html
点击GET /admin/hosp/hospitalSet/getHospSEt/{id}
输入表中没有的id号,除0外
比如:id为1111111
点击try it out后,显示结果为
Response Body
{
"code": 500,
"message": "失败",
"data": null,
"ok": false
}
============2.1 加入HospitalSetController类==================
@ApiOperation(value = "根据id获取医院设置")
@GetMapping("getHospSEt/{id}")
public Result getHospitalSet(@PathVariable Long id){
int aa=2/0;
HospitalSet hospitalSet = hospitalSetService.getById(id);
return Result.ok(hospitalSet);
}
===========2.2 加入GlobalExceptionHandler类================
package com.koki.yygh.common.exception;
import com.koki.yygh.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
@SuppressWarnings("ALL")
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public Result error(Exception e){
e.printStackTrace();
return Result.fail();
}
}
=======2.3 测试===【全局异常处理类】===================
浏览器中访问http://localhost:8201/swagger-ui.html
点击GET /admin/hosp/hospitalSet/getHospSEt/{id}
输入表中没有的id号,除0外
比如:id为1111111
点击try it out后,显示结果为
Response Body
{
"code": 201,
"message": "失败",
"data": null,
"ok": false
}
============3.1 HospitalSetController类==================
@ApiOperation(value = "根据id获取医院设置")
@GetMapping("getHospSEt/{id}")
public Result getHospitalSet(@PathVariable Long id){
try{【加入这些异常代码】
int aa=2/0;
}catch(Exception e){
throw new YyghException("失败",201);
}
HospitalSet hospitalSet = hospitalSetService.getById(id);
return Result.ok(hospitalSet);
}
===========3.2 GlobalExceptionHandler类加入以下的代码================
@ExceptionHandler(YyghException.class)
@ResponseBody
public Result error(YyghException e){
e.printStackTrace();
return Result.build(e.getCode(), e.getMessage());
}
=======3.3加入 YyghException类======================
package com.koki.yygh.common.exception;
import com.koki.yygh.common.result.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@SuppressWarnings("ALL")
@Data
@ApiModel(value = "自定义全局异常类")
public class YyghException extends RuntimeException {
@ApiModelProperty(value = "异常状态码")
private Integer code;
public YyghException(String message, Integer code) {
super(message);
this.code = code;
}
public YyghException(ResultCodeEnum resultCodeEnum) {
super(resultCodeEnum.getMessage());
this.code = resultCodeEnum.getCode();
}
@Override
public String toString() {
return "YyghException{" +
"code=" + code +
", message=" + this.getMessage() +
'}';
}
}
=======3.4 测试====【自定义异常处理方法】==================
浏览器中访问http://localhost:8201/swagger-ui.html
点击GET /admin/hosp/hospitalSet/getHospSEt/{id}
输入表中没有的id号,除0外
比如:id为1111111
点击try it out后,显示结果为
Response Body
{
"code": 201,
"message": "失败",
"data": null,
"ok": false
}