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
    评论
### 回答1: 可能是以下原因导致的: 1. 静态资源路径不正确:请确保静态资源的路径正确,可以在浏览器手动输入路径来检查是否能够访问到静态资源。 2. 静态资源未放置在正确的目录下:请确保静态资源放置在Spring Boot默认的静态资源目录下,即src/main/resources/static/目录下。 3. 静态资源未被正确配置:请在application.properties或application.yml配置静态资源的路径,例如: ``` # application.properties spring.resources.static-locations=classpath:/static/ # application.yml spring: resources: static-locations: classpath:/static/ ``` 如果以上方法都无法解决问题,请提供更多的信息,例如错误提示信息、代码片段等,以便更好地帮助您解决问题。 ### 回答2: Spring Boot是一个开发框架,它提供了很多便捷的方式来构建Java Web应用程序。在Spring Boot,默认情况下,静态资源的路径是在classpath下的/static或/public或/resources或/META-INF/resources文件夹。如果我们在这些文件夹添加了静态资源,那么应用程序就可以通过相对路径来访问这些静态资源。 如果Spring Boot应用程序无法访问静态资源,可能有以下几个原因: 1. 静态资源文件放置位置不正确:请确保静态资源文件放置在类路径上的正确位置,即/static或/public或/resources或/META-INF/resources文件夹。 2. 静态资源文件命名不正确:请确保静态资源文件的命名正确,并且包含文件扩展名。例如,对于一个名为"index.html"的HTML文件,请确保文件名和扩展名都正确。 3. 静态资源文件路径错误:请确保在访问静态资源时,使用了正确的路径。相对路径和绝对路径都可以使用,具体取决于你的应用程序配置。 4. 静态资源文件被过滤或限制访问:有时候,服务器可能会有配置或规则来过滤或限制对某些文件类型或路径的访问。请检查服务器的配置和规则,确保没有禁止访问静态资源。 如果按照以上步骤仍然无法解决问题,建议检查应用程序的日志信息,看是否有相关错误或异常信息。通过排查错误信息,我们可以更好地理解问题的原因,并能够采取适当的措施来解决问题。 总之,Spring Boot访问不到静态资源可能是由于资源文件位置不正确、命名错误、路径错误或者被过滤限制访问等原因导致的。要解决这个问题,我们需要检查这些方面并采取适当的措施来纠正。 ### 回答3: springboot默认的静态资源路径为`/static`、`/public`、`/resources`、`/META-INF/resources`,当我们将静态资源放在这些路径时,可以直接通过相对路径访问静态资源。 如果springboot无法访问到静态资源,可能有以下几个原因: 1. 静态资源没有放在默认的路径:请确保静态资源文件夹是放在了`/static`、`/public`、`/resources`、`/META-INF/resources`这些路径,或者在`application.properties`或者`application.yml`配置了自定义的静态资源路径。 2. 静态资源被拦截器拦截:如果应用有自定义的拦截器,可能会拦截静态资源请求。需要在拦截器添加排除静态资源的配置。 3. 静态资源处理器未配置:在`WebMvcConfig`类,需要添加`@EnableWebMvc`注解,并重写`addResourceHandlers`方法,来自定义静态资源处理器。 4. 静态资源访问权限问题:如果静态资源文件夹是放在了`resources`目录下,可能存在资源文件夹的访问权限问题。可以尝试使用绝对路径或者修改资源文件夹的权限。 以上是常见的一些原因,可以根据具体情况进行排查和解决。希望对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值