本地环境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项目
相关推荐
颜酱3 分钟前
03 | 实现节点1 — 抽取关键词
前端·人工智能·后端
整點薯條32 分钟前
Vue3+vite创建项目架构文件说明总结
前端·javascript·vue.js
掘金一周1 小时前
想问问掘友们:AI 时代,资深开发的核心价值到底在哪里?| 沸点周刊 7.23
前端·人工智能·后端
橘子星1 小时前
🚀 手把手带你写一个端侧 AI 应用的"加载进度条"与"聊天对话框"
前端·人工智能
37.2℃9952 小时前
专业的Claude Design解决方案
前端·算法
Jolyne_2 小时前
ng-zorro-table列宽resize后宽度是NaN的源码调试记录
前端
liwulin05062 小时前
【ollama】自定义结构化输出
linux·前端·数据库·ollama
豆瓣鸡2 小时前
Guava RateLimiter 限流实战:从令牌桶原理到 Nacos 动态配置
spring boot·微服务·nacos·guava
前端甜糖2 小时前
记录We码开发者工具的一个bug
前端·javascript
程序员黑豆2 小时前
鸿蒙应用开发教程:以红绿灯切换为例,掌握条件渲染的核心用法
前端·harmonyos