spring-boot-devtools的范围设置为provided,打包的时候不会打包这个jar包,防止异常时泄露信息,具体见参考连接。
配置文件4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.2 com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools provided true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
server.servlet.context-path的值为/api时,如果访问的地址不是以api开头将不会进入到异常捕捉。所以最好是为/
server.servlet.context-path=/api spring.jackson.default-property-inclusion=non_null spring.mvc.throw-exception-if-no-handler-found=true spring.web.resources.add-mappings=false全局异常处理
@Slf4j
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = Exception.class)
public Result exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
log.error("内部错误", e);
return new Result(500, "Internal Server Error", null);
}
}
结果实体
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private int code;
private String message;
private Object data;
}
Controller
@RestController
@RequestMapping("test")
public class TestController {
@RequestMapping
public @ResponseBody Result test() {
return new Result(200, "OK", null);
}
}
参考连接:https://blog.csdn.net/w1014074794/article/details/106038996/



