书史足自悦,安用勤与劬。这篇文章主要讲述访问webapp目录报404错误相关的知识,希望能为你提供帮助。
spring boot打包后,访问webapp目录报了404.并且java包中没有前端代码
解决办法:
1、 配置pom.xml
< build> < resources> < resource> < directory> src/main/webapp< /directory> < !--注意此次必须要放在此目录下才能被访问到 --> < targetPath> META-INF/resources< /targetPath> < includes> < include> **/**< /include> < /includes> < /resource> < resource> < directory> src/main/resources< /directory> < filtering> true< /filtering> < includes> < include> **/*< /include> < /includes> < /resource> < /resources> < /build>
2、 重写WebMvcConfigurerAdapter适配器 addResourceHandlers方法
package com.iwhalecloud.visualization.config; import java.nio.charset.Charset; import java.util.List; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * Created by 61990 on 2019/1/14. */ @Configuration @EnableWebMvc @ComponentScan public class PortalWebConfig extends WebMvcConfigurerAdapter{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"); registry.addResourceHandler("/views/**").addResourceLocations("classpath:/static/main/"); // 指到 webapp 目录下 registry.addResourceHandler("/webcontent/**").addResourceLocations("/webcontent/"); }/** * 自定义静态资源映射 */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }
//解决后端代码传到前端时,中文乱码问题 @Bean public HttpMessageConverter< String> responseBodyConverter(){ StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8")); return converter; }@Override public void configureMessageConverters(List< HttpMessageConverter< ?> > converters) { super.configureMessageConverters(converters); converters.add(responseBodyConverter()); } }
【访问webapp目录报404错误】
推荐阅读
- [Spring boot] Application properties and configurations
- RxSwift + Moya + ObjectMapper
- 安卓手机无线投屏电脑 三种方法轻松搞
- 如何修复Windows 10未检测到笔记本电脑摄像头(解决办法)
- 如何修复Windows 10 iaStorA.sys BSOD错误(7种方法分步指南)
- 如何修复Xbox One耳机不工作的问题(解决办法教程)
- 如何修复Windows 10麦克风太安静的问题(解决办法介绍)
- Windows 10如何修复触摸板滚动不起作用(解决办法介绍)
- 如何修复Windows 11中的空白图标(解决办法教程)