You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot项目网页无法显示图片,提示无GET image/image.png端点映射求助

SpringBoot网页图片加载失败解决办法

排查方向及修复方案

1. 检查静态资源访问路径

SpringBoot默认从classpath:/static/、classpath:/public/这些目录加载静态资源。如果你的图片放在static/image/image.png,访问路径必须是/image/image.png(开头的斜杠不能少,否则会变成相对当前页面的路径,导致找不到端点)。

要是需要自定义静态资源目录,直接在配置文件里加:

# application.properties
spring.web.resources.static-locations=classpath:/my-static/,classpath:/static/

2. 避免控制器路径冲突

检查项目里的@Controller或@RestController,有没有写过/image/**这类映射,这会抢占静态资源的默认访问路径。要么改控制器的路径,要么调整静态资源的访问前缀。

3. 放行静态资源拦截

如果用了Spring Security或者自定义拦截器,必须把图片路径加入放行名单。比如Spring Security的配置:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/image/**").permitAll()
                .anyRequest().authenticated();
    }
}

4. 核对文件实际位置

确认图片确实存在于指定目录,路径和文件名别写错,Linux环境下注意大小写敏感,比如Image.png和image.png是两个不同文件。

5. 手动添加资源映射

如果上面的方法都没用,直接写个控制器手动映射:

@Controller
public class ImageController {
    @GetMapping("/image/**")
    public ResponseEntity<Resource> loadImage(HttpServletRequest request) {
        String imgName = request.getRequestURI().replace("/image/", "");
        Resource imgResource = new ClassPathResource("static/image/" + imgName);
        if (!imgResource.exists()) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(imgResource);
    }
}

内容的提问来源于stack exchange,提问作者Đức

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 22:49:54