本地环境vue与springboot联调

方案1: 前后端分离

  1. 启动Springboot服务
  2. 配置vue代理(前端项目根目录下的vue.config.js),转发API请求到后端服务
javascript 复制代码
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:2025', // Spring Boot 服务地址
        changeOrigin: true, // 支持跨域
        // pathRewrite: {
        //   '^/api': '', // 根据实际需求调整路径重写规则
        // },
      },
    },
  },
})
  1. 启动vue服务

方案2:前后端不分离

  1. 为Spring配置静态资源路径,指向vue项目的编译结果文件夹
bash 复制代码
-Dspring.resources.static-locations=classpath:/static/,file:/path/to/static/
  1. 如果使用的是较新的 Spring Boot 版本(如 2.4+)
bash 复制代码
spring的默认路径为:
            "classpath:/META-INF/resources/",
            "classpath:/resources/",
            "classpath:/static/",
            "classpath:/public/"
-Dspring.web.resources.static-locations=file:/absolute/path/to/static/dist/
  1. Spring中配置所有非API请求转发到vue的index页面
java 复制代码
@Controller
public class IndexController {

    /**
     * desc @RequestMapping("/{path:[^\\.]*}"):将非 API 的请求路径转发到 index.html。
     * 前提是你的 index.html 文件在 src/main/resources/static 或其他静态资源目录下。
     * @return :
     */
    @RequestMapping("/{path:[^\\.]*}")
    public String forwardToIndex() {
        return "forward:/index.html";
    }
}
  1. 启动Springboot项目
相关推荐
Moment2 分钟前
富文本编辑器技术选型,到底是 Prosemirror 还是 Tiptap 好 ❓❓❓
前端·javascript·面试
xkxnq7 分钟前
第二阶段:Vue 组件化开发(第 18天)
前端·javascript·vue.js
晓得迷路了8 分钟前
栗子前端技术周刊第 112 期 - Rspack 1.7、2025 JS 新星榜单、HTML 状态调查...
前端·javascript·html
怕浪猫11 分钟前
React从入门到出门 第五章 React Router 配置与原理初探
前端·javascript·react.js
jinmo_C++11 分钟前
从零开始学前端 · HTML 基础篇(一):认识 HTML 与页面结构
前端·html·状态模式
鹏多多18 分钟前
前端2025年终总结:借着AI做大做强再创辉煌
前端·javascript
Coder_Boy_18 分钟前
Spring Boot 事务回滚异常 UnexpectedRollbackException 详解(常见问题集合)
java·spring boot·后端
WebGISer_白茶乌龙桃23 分钟前
Cesium实现“悬浮岛”式,三维立体的行政区划
javascript·vue.js·3d·web3·html5·webgl
小Tomkk26 分钟前
⭐️ StarRocks Web 使用介绍与实战指南
前端·ffmpeg
期待のcode27 分钟前
TransactionManager
java·开发语言·spring boot