一、静态资源
Spring Boot 中默认提供了五个静态资源存储路径:
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
- /(webapp)
五个位置,优先级依次降低。
如果需要自定义静态资源位置,有两种方式:
-
application.properties 中进行配置
spring.mvc.static-path-pattern=/static/** spring.web.resources.static-locations=classpath:static/
-
Java代码配置
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("classpath:static/"); } }
二、异常处理
2.1 实现 HandlerExceptionResolver 、
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class GlobalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
ModelAndView view = new ModelAndView("error");
view.addObject("error", ex.getMessage());
return view;
}
}
2.2 使用 @ExceptionHandler (推荐)
2.2.1 @ControllerAdvice注解
三种用法:
1. 全局异常处理
2. 定义全局数据
3. 请求参数预处理
package com.ahao.exception.exception;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalException2 {
@ExceptionHandler(Exception.class)
public String error(Exception e, Model model) {
model.addAttribute("error", e.getMessage());
return "error";
}
}
三、自定义异常数据
3.1
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;
import java.util.Map;
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options);
errorAttributes.put("aaa", "bbb");
return errorAttributes;
}
}
3.2
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.DefaultErrorViewResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
//@Component
public class MyErrorViewResolver extends DefaultErrorViewResolver {
/**
*
* @param applicationContext
* @param webProperties
*/
public MyErrorViewResolver(ApplicationContext applicationContext, WebProperties webProperties) {
super(applicationContext, webProperties.getResources());
}
@Override
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
ModelAndView mv = new ModelAndView();
mv.setViewName("aaa/error");
Map<String, Object> map = new HashMap<>();
Set<String> keySet = model.keySet();
for (String key : keySet) {
map.put(key, model.get(key));
}
map.put("message", "出错啦");
mv.addAllObjects(map);
return mv;
}
}