当前位置: 首页 > news >正文

springboot web 增加不存在的url返回200状态码 vue 打包设置

spring boot项目增加 html web页面访问

1. 首先 application.properties 文件中增加配置,指定静态资源目录(包括html的存放)

spring.resources.static-locations=classpath:/webapp/,classpath:/webapp/static/

2. 项目目录

3. 如果有实现 WebMvcConfigurer  类的,增加实现

package yourpack;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import javax.annotation.Resource;/*** @author wangtong*/
@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {@Autowiredprivate YourInterceptor yourint;// 拦截器@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(yourint).addPathPatterns("/url").addPathPatterns("/url2");// 不设置拦截器的可以不实现该方法}@Overridepublic void addViewControllers(ViewControllerRegistry registry) {
//        registry.addViewController("/").setStatusCode(HttpStatus.OK);registry.addViewController("/index.jsp").setViewName("index"); // 配置首页registry.addViewController("/").setViewName("index");registry.addViewController("/404").setViewName("404"); // 配置404页面}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {
// 设置静态资源目录registry.addResourceHandler("/static/**").addResourceLocations("classpath:/webapp/static/").addResourceLocations("classpath:/resources/webapp/static/");}
}

如果访问不到页面的,可以检查下application配置文件是否有以下配置

#spring.web.resources.add-mappings=false
#spring.resources.add-mappings=false

如果有的话,需要进行注释。这两个配置都是不进行静态资源的映射。所以会导致html等无法访问。

增加spring boot web不存在的url返回200状态码

1. application配置文件增加以下配置

spring.mvc.throw-exception-if-no-handler-found=true

2. 增加一个error配置类的实现

package ;import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;@Configuration
public class ErrorConfig implements ErrorPageRegistrar {@Overridepublic void registerErrorPages(ErrorPageRegistry registry) {ErrorPage[] errorPages = new ErrorPage[1];errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, "/404.do");registry.addErrorPages(errorPages);}
}

3. 增加一个mapping

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;@RequestMapping(value = {"/404.do"})public ResponseEntity<Map<String, String>> error() {Map<String, String> retMap = new HashMap<>();retMap.put("message", "请求路径不存在");return new ResponseEntity<Map<String, String>> (retMap, HttpStatus.OK);}

访问一下

vue 打包配置:

1. main.js 配置 axios相关,这里没有进行增加前缀路由,注释调的api是增加的,但是打包后,访问的页面里面也加上了,不知道为什么,所有就去掉吧

// var baseURL = '/api';
var baseURL = 'http://localhost:8080';
axios.interceptors.request.use(config=>{config.baseURL= baseURL;config.headers.post["Origin"] = baseURL;config.headers.post["Referer"] = baseURL;return config;
});
axios.defaults.withCredentials = true;
axios.defaults.headers.post["Origin"] = baseURL;
axios.defaults.headers.post["Referer"] = baseURL;Vue.prototype.$request=axios;

2. package.json 文件, scripts 中没有build的可以增加一个,如果执行 npm run build 报错的,可以改成build+后缀的其它。 我这里的话 npm run buildt

{"name": "vue-admin-template","version": "4.4.0","description": "A vue admin template with Element UI & axios & iconfont & permission control & lint","author": "","scripts": {"dev": "vue-cli-service serve","build:prod": "vue-cli-service build","buildt": "npm install && vue-cli-service build","preview": "node build/index.js --preview","svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml","lint": "eslint --ext .js,.vue src","test:unit": "jest --clearCache && vue-cli-service test:unit","test:ci": "npm run lint && npm run test:unit"},

3. vue.config.js文件

module.exports = {publicPath: '/',outputDir: '../your-web/src/main/resources/webapp',assetsDir: 'static',lintOnSave: process.env.NODE_ENV === 'development',productionSourceMap: false,devServer: {port: 2234,open: true,overlay: {warnings: false,errors: true},// proxy: { //   '/api': { //     target: 'http://localhost:8080',//     ws: true,//     changeOrigin: true ,//     pathRewrite:{//       '^/api':''//     }//   } // }},

这里的话,axios没有设置前缀,所以这里的路由也就不需要了。注释掉。

outputDir  要输出的目录路径,这里的话,我这里打包的不在当前这个目录下面。

生成到和当前node父目录同层的指定目录下。

http://www.lryc.cn/news/159647.html

相关文章:

  • JavaWeb_LeadNews_Day11-KafkaStream实现实时计算文章分数
  • python tcp server client示例代码
  • typecho 反序列化漏洞复现
  • Python实现SSA智能麻雀搜索算法优化LightGBM分类模型(LGBMClassifier算法)项目实战
  • Java多线程4种拒绝策略
  • MySQL的MHA
  • Java实现链表
  • SpringCloud Alibaba(2021.0.1版本)微服务-OpenFeign以及相关组件使用(保姆级教程)
  • 豆制品废水处理设备源头厂家方案
  • lnmp环境搭建
  • 全球研发中心城市专题协商会课题调研组莅临麒麟信安考察指导
  • ZeroTier客户端连接服务器
  • NFT Insider#106:The Sandbox 与 Light Matrix 以及鲁比尼拳击场达成战略合作
  • 【猿灰灰赠书活动 - 04期】- 【分布式统一大数据虚拟文件系统——Alluxio原理、技术与实践】
  • 前端element表格导出excel
  • React中的类组件和函数组件(详解)
  • 1987-2021年全国31省专利申请数和授权数
  • 欧洲云巨头OVHcloud收购边缘计算专家 gridscale
  • java从入门到起飞(八)——循环和递归
  • 架构师成长之路|Redis实现延迟队列的三种方式
  • 51单片机智能电风扇控制系统proteus仿真设计( 仿真+程序+原理图+报告+讲解视频)
  • 【设计模式】Head First 设计模式——工厂方法模式 C++实现
  • 【爬虫】7.2. JavaScript动态渲染界面爬取-Selenium实战
  • c语言实训心得3篇集合
  • 2023高教社杯数学建模B题思路代码 - 多波束测线问题
  • MySql 变量
  • 2023-简单点-make和build都是什么东西?
  • Nginx 学习(八)Nginx实现用IP测试灰度发布
  • QT 自定义信号
  • 注解方式配置SpringMVC