静态资源
1.默认映射目录:
classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/。
2.设置springboot的配置文件来进行其他目录访问
yml文件中添加配置:
spring:
resources:
static-locations:
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
- classpath:/img/
- classpath:/js/
全局异常
1.Springboot对Controller层方法进行统一异常处理
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
/**
* Created by 86152 on 2020/1/6.
*/
@RestControllerAdvice
public class ControllerExceptionHandleAdvice {
private final static Logger logger = LoggerFactory.getLogger(ControllerExceptionHandleAdvice.class);
@ExceptionHandler
public String handler(HttpServletRequest req, HttpServletResponse res, Exception e) {
logger.info("Restful Http请求发生异常...");
if (res.getStatus() == HttpStatus.BAD_REQUEST.value()) {
logger.info("修改返回状态值为200");
res.setStatus(HttpStatus.OK.value());
}
if (e instanceof NullPointerException) {
logger.error("代码00:" + e.getMessage(), e);
return "发生空指针异常";
} else if (e instanceof IllegalArgumentException) {
logger.error("代码01:" + e.getMessage(), e);
return "请求参数类型不匹配";
} else if (e instanceof SQLException) {
logger.error("代码02:" + e.getMessage(), e);
return "数据库访问异常";
} else {
logger.error("代码99:" + e.getMessage(), e);
return "服务器代码发生异常,请联系管理员";
}
}
}