本地环境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项目
相关推荐
Eloudy6 小时前
ReAct 原理简介
前端·javascript·人工智能·react.js·agent·gpu
ZJU_统一阿萨姆7 小时前
【DSH】如何将 DeepSeek Harness 嵌入生产环境?万字解构 DeepSeek Agent Harness 的运行机制与安全边界
前端·javascript·安全
用户921080262867 小时前
4. 前端地图大量点位实时更新优化:后端推增量,前端做 diff
前端
SoaringHeart7 小时前
Flutter最佳实践:Web项目中文字体首次加载乱码问题解决
前端·flutter
西安小哥8 小时前
破局与重生:大厂前端如何借力 AI 转型“超级全栈“
前端·人工智能
sunly_9 小时前
TypeScript总结:16、面向对象速查
前端·javascript·typescript
MXN_小南学前端9 小时前
React超长文本域中实现“返回顶部”浮动按钮
前端·javascript·react.js
学习嵌入式的小周9 小时前
Notepad++8.8.7下载安装(附安装包)
前端·notepad++
gs801409 小时前
解构 Cordis:面向“时空可组合性”的 TypeScript 元框架深度剖析
前端·javascript·typescript
岁岁种桃花儿10 小时前
Vue核心语法第一篇:Vue是什么?
前端·javascript·vue.js