注意几个注解:@ControllerAdvice @ExceptionHandler @ResponseBody
package com.sayhello.sayhello.Handle;
import com.sayhello.sayhello.Exception.GirlException;
import com.sayhello.sayhello.Utils.ResultUtils;
import com.sayhello.sayhello.domain.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class GirlHandle {
@ExceptionHandler(value = GirlException.class)
@ResponseBody
public Result handleException(GirlException e){
//System.out.println(e.getMessage());
return ResultUtils.error(e.getCode(),e.getMessage());
}
}