前端使用 Vue 3,后端使用 Spring Boot 构建 Hello World 程序
创始人
2025-01-10 21:35:18
0

前端使用 Vue 3,后端使用 Spring Boot 构建 Hello World 程序

前端(Vue 3)

首先,创建一个 Vue 3 项目。

1. 安装 Vue CLI

npm install -g @vue/cli 

2. 创建 Vue 项目

vue create frontend 

在交互式提示中,选择默认的 Vue 3 预设。

3. 修改 App.vue

frontend/src 目录下,修改 App.vue 文件:

     

4. 运行前端项目

cd frontend npm run serve 

后端(Spring Boot)

接下来,创建一个 Spring Boot 项目。

1. 使用 Spring Initializr 创建项目

可以通过 Spring Initializr 创建项目,选择以下配置:

  • Project: Maven
  • Language: Java
  • Spring Boot:
  • Dependencies: Spring Web

下载项目并解压缩,或者通过命令行工具生成项目。

2. 创建 Spring Boot 项目结构

在项目的 src/main/java/com/example/demo 目录下,创建一个控制器类 HelloController.java

package com.example.demo;  import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;  @RestController public class HelloController {      @GetMapping("/api/hello")     public Message hello() {         return new Message("Hello, World!");     }      public static class Message {         private String message;          public Message(String message) {             this.message = message;         }          public String getMessage() {             return message;         }          public void setMessage(String message) {             this.message = message;         }     } } 

3. 创建 CORS 配置类

在项目的 src/main/java/com/example/demo 目录下,创建一个配置类 CorsConfig.java

package com.example.demo;  import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;  @Configuration public class CorsConfig {      @Bean     public WebMvcConfigurer corsConfigurer() {         return new WebMvcConfigurer() {             @Override             public void addCorsMappings(CorsRegistry registry) {                 registry.addMapping("/api/**")                         .allowedOrigins("http://localhost:8081")                         .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD")                         .allowCredentials(true);             }         };     } } 

4. 运行 Spring Boot 应用

在项目根目录下运行:

./mvnw spring-boot:run 

整合前后端

确保 Vue 应用和 Spring Boot 应用同时运行。前端 Vue 应用将在 http://localhost:8081 上运行,后端 Spring Boot 应用将在 http://localhost:8080 上运行。

前端应用将通过 fetch 请求从后端获取数据,并显示在页面上。

项目结构

确保项目结构如下:

project-directory/ │ ├── frontend/ │   ├── public/ │   ├── src/ │   │   └── App.vue │   ├── package.json │   └── ... │ ├── backend/ │   ├── src/ │   │   ├── main/ │   │   │   ├── java/com/example/demo/ │   │   │   │   └── HelloController.java │   │   │   │   └── CorsConfig.java │   │   │   └── resources/ │   │   ├── test/ │   │   └── ... │   ├── mvnw │   ├── mvnw.cmd │   ├── pom.xml │   └── ... 

验证

  1. 启动前端和后端应用

    • 前端:npm run serve
    • 后端:./mvnw spring-boot:run
  2. 访问 Vue 应用:在浏览器中打开 http://localhost:8081,查看是否显示 “Hello, World!”。

相关内容

热门资讯

透视最新"hhpok... 透视最新"hhpoker开挂教程"本来是真的有挂(透视)新版2025教程(有挂教程)所有人都在同一条...
透视黑科技!aapoker真的... 透视黑科技!aapoker真的假的(透视)免费透视脚本(原来是有挂)1.aapoker真的假的 ai...
透视模拟器!wpk真的有透视嘛... 透视模拟器!wpk真的有透视嘛,竟然真的是有挂(透视)wpk教程(有挂黑科技)1、完成wpk真的有透...
透视有挂"hhpok... 透视有挂"hhpoker免费透视脚本"果然有挂(透视)细节揭秘(有挂揭秘)hhpoker免费透视脚本...
透视实锤!aapoker透视脚... 透视实锤!aapoker透视脚本(透视)脚本(真是有挂);1、进入到aapoker透视脚本黑科技之后...
透视攻略!aapoker透视脚... 透视攻略!aapoker透视脚本下载,原先真的有挂(透视)细节揭秘(有挂技巧);1、游戏颠覆性的策略...
透视数据"约局吧怎么... 透视数据"约局吧怎么看有没有挂"本来真的是有挂(透视)科技教程(有挂技巧);1、在约局吧怎么看有没有...
透视能赢!aapoker怎么设... 透视能赢!aapoker怎么设置提高好牌几率(透视)透视脚本下载(原来真的是有挂);1、aapoke...
透视有挂!hhpoker透视脚... 透视有挂!hhpoker透视脚本视频,其实是有挂(透视)微扑克教程(有挂辅助)1、hhpoker透视...
透视安装"wepok... 透视安装"wepoker透视脚本免费下载pc"切实是有挂(透视)2025教程(有挂细节)1)wepo...