修改了controller
package com.example.springio;
import com.example.springio.exeption.myException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.atomic.AtomicLong;
@RestController
public class GreetingController {
private final String temple = "hello %s";
private final AtomicLong conter = new AtomicLong();
@GetMapping("/Greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name){
return new Greeting(conter.incrementAndGet(),String.format(temple,name));
}
@GetMapping("/login")
public Greeting login(@RequestParam(value = "name", defaultValue = "World") String name){
if(name.equals("a")){
throw new myException("用户名错误","401");
}else{
int a = 5/0;
}
return new Greeting(conter.incrementAndGet(),String.format(temple,name));
}
}
此时这里触发了代码异常
执行了exception
package com.example.springio;
import com.example.springio.exeption.myException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@ControllerAdvice
public class exception {
@ExceptionHandler(Exception.class)
@ResponseBody
public Object exception(Exception e){
Map<String,Object> map = new HashMap<>();
map.put("success",false);
map.put("code","ERR000");
map.put("messege",e.getMessage());
return map;
}
@ExceptionHandler(myException.class)
@ResponseBody
public Object myexception(myException e){
Map<String,Object> map = new HashMap<>();
map.put("success",false);
map.put("code","ERR001");
map.put("messege",e.getMessage());
log.debug(getlog(e));
return map;
}
public String getlog(Exception e){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
}
然后尝试抛出自定义异常
此时调用的为
if(name.equals("a")){
throw new myException("用户名错误","401");
@ExceptionHandler(myException.class)
@ResponseBody
public Object myexception(myException e){
Map<String,Object> map = new HashMap<>();
map.put("success",false);
map.put("code","ERR001");
map.put("messege",e.getMessage());
log.debug(getlog(e));
return map;
}
为了把堆栈信息打印到log
引入了@Slf4j
这样可以调用
log.debug()
这里封装一个方法把exception转化成string
public String getlog(Exception e){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
最后附上myExeption,注意继承了RuntimeException
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class myException extends RuntimeException{
private String code;
public myException(String message, String code) {
super(message);
this.code = code;
}
public myException(String message, Throwable cause, String code) {
super(message, cause);
this.code = code;
}
}