提交 d6f54060 编写于 作者: E eknown

📝 上传SpringCloud系列项目

上级 1c168481
package com.example.clientdemo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zfh
* @version 1.0
* @since 2019/10/16 10:21
*/
@RestController
@RequestMapping(value = "")
public class HelloAction {
@GetMapping(value = "")
public String index() {
return "this is index page, you can request /hello to get other fallback.";
}
@GetMapping(value = "hello")
public String hello() {
return "hello.";
}
}
package com.example.clientdemo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zfh
* @version 1.0
* @since 2019/10/16 10:21
*/
@RestController
@RequestMapping(value = "")
public class HelloAction {
@GetMapping(value = "")
public String index() {
return "this is index page, you can request /hello to get other fallback.";
}
@GetMapping(value = "hello")
public String hello() {
return "hello.";
}
}
# SpringBoot Controller统一异常处理
---
# SpringBoot Controller统一异常处理
---
该项目为SpringBoot Web应用对,针对所有Controller实现的统一异常处理,可以将异常锁死在后台应用,方便调用者的统一处理。
\ No newline at end of file
package com.example.exceptiondemo.config;
import com.example.exceptiondemo.exception.UserNotExistException;
import com.example.exceptiondemo.model.JsonResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* @author zfh
* @version 1.0
* @since 2019/12/5 15:43
*/
@ControllerAdvice
public class ControllerHandler {
/**
* 处理全部异常
* @param ex
* @return
*/
@ExceptionHandler
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public JsonResult handleException(Exception ex) {
System.out.println("程序异常:" + ex.toString());
return new JsonResult(false, "请求失败");
}
/**
* 处理UserNotExistException异常
* @param ex
* @return
*/
@ExceptionHandler({UserNotExistException.class})
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public JsonResult handleUserNotExistException(UserNotExistException ex) {
System.out.println("请求用户数据异常:" + ex.toString());
return new JsonResult(false, "请求用户数据失败");
}
}
package com.example.exceptiondemo.config;
import com.example.exceptiondemo.exception.UserNotExistException;
import com.example.exceptiondemo.model.JsonResult;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* @author zfh
* @version 1.0
* @since 2019/12/5 15:43
*/
@ControllerAdvice
public class ControllerHandler {
/**
* 处理全部异常
* @param ex
* @return
*/
@ExceptionHandler
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public JsonResult handleException(Exception ex) {
System.out.println("程序异常:" + ex.toString());
return new JsonResult(false, "请求失败");
}
/**
* 处理UserNotExistException异常
* @param ex
* @return
*/
@ExceptionHandler({UserNotExistException.class})
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public JsonResult handleUserNotExistException(UserNotExistException ex) {
System.out.println("请求用户数据异常:" + ex.toString());
return new JsonResult(false, "请求用户数据失败");
}
}
package com.example.exceptiondemo.controller;
import com.example.exceptiondemo.exception.UserNotExistException;
import com.example.exceptiondemo.model.JsonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zfh
* @version 1.0
* @since 2019/12/5 15:33
*/
@RestController
@RequestMapping(value = "")
public class IndexAction {
@GetMapping(value = "add")
public Integer add(Integer a, Integer b) {
return a+b;
}
@GetMapping(value = "user/{id}")
public JsonResult findUser(@PathVariable Integer id) throws UserNotExistException {
if (id < 100) {
throw new UserNotExistException(id);
}
return new JsonResult(true, "暂未实现用户查询功能");
}
}
package com.example.exceptiondemo.controller;
import com.example.exceptiondemo.exception.UserNotExistException;
import com.example.exceptiondemo.model.JsonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zfh
* @version 1.0
* @since 2019/12/5 15:33
*/
@RestController
@RequestMapping(value = "")
public class IndexAction {
@GetMapping(value = "add")
public Integer add(Integer a, Integer b) {
return a+b;
}
@GetMapping(value = "user/{id}")
public JsonResult findUser(@PathVariable Integer id) throws UserNotExistException {
if (id < 100) {
throw new UserNotExistException(id);
}
return new JsonResult(true, "暂未实现用户查询功能");
}
}
package com.example.exceptiondemo.exception;
/**
* @author zfh
* @version 1.0
* @since 2019/12/5 16:00
*/
public class UserNotExistException extends Exception {
private static final long serialVersionUID = -2373528888948315963L;
private Integer id;
public UserNotExistException(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "用户不存在";
}
}
package com.example.exceptiondemo.exception;
/**
* @author zfh
* @version 1.0
* @since 2019/12/5 16:00
*/
public class UserNotExistException extends Exception {
private static final long serialVersionUID = -2373528888948315963L;
private Integer id;
public UserNotExistException(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "用户不存在";
}
}
package com.example.exceptiondemo.model;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* @author zfh
* @version 1.0
* @since 2019/12/5 15:44
*/
public class JsonResult {
protected boolean success;
protected String msg;
protected int code;
protected Object data;
public JsonResult() {
}
public JsonResult(boolean success, String msg) {
this(success);
this.msg = msg;
}
public JsonResult(boolean success, String msg, int code) {
this(success, msg);
this.code = code;
}
public JsonResult(boolean success) {
this();
this.success = success;
}
public JsonResult(boolean success, String msg, Object data) {
this(success, msg);
this.data = data;
}
public boolean getSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {