本地环境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项目
相关推荐
Laravel技术社区30 分钟前
用PHP8实现斗地主游戏,实现三带一,三带二,四带二,顺子,王炸功能(第二集)
前端·游戏·php
m0_738120722 小时前
应急响应——知攻善防Web-3靶机详细教程
服务器·前端·网络·安全·web安全·php
派大鑫wink8 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
我是小路路呀8 小时前
element级联选择器:已选中一个二级节点,随后又点击了一个一级节点(仅浏览,未确认选择),此时下拉框失去焦点并关闭
javascript·vue.js·elementui
程序员爱钓鱼8 小时前
Node.js 编程实战:文件读写操作
前端·后端·node.js
xUxIAOrUIII8 小时前
【Spring Boot】控制器Controller方法
java·spring boot·后端
PineappleCoder9 小时前
工程化必备!SVG 雪碧图的最佳实践:ID 引用 + 缓存友好,无需手动算坐标
前端·性能优化
Dolphin_Home9 小时前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法
JIngJaneIL9 小时前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
敲敲了个代码9 小时前
隐式类型转换:哈基米 == 猫 ? true :false
开发语言·前端·javascript·学习·面试·web