SpringBoot中的静态资源和异常处理问题

一、静态资源

Spring Boot 中默认提供了五个静态资源存储路径:

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/
  • /(webapp)

五个位置,优先级依次降低。

如果需要自定义静态资源位置,有两种方式:

  1. application.properties 中进行配置

    spring.mvc.static-path-pattern=/static/**
    spring.web.resources.static-locations=classpath:static/
    
  2. 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值