前端使用 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!”。

相关内容

热门资讯

做出回应!hhpoker怎么防... 做出回应!hhpoker怎么防作弊,哈局透视辅助,模板教程(讲解有挂)1、哈局透视辅助有没有辅助教程...
黑科技辅助挂!智星菠萝有挂吗,... 黑科技辅助挂!智星菠萝有挂吗,雀友会鱼虾蟹下载官网辅助器,课程教程(有挂存在)1、超多福利:超高返利...
针对!wepoker有没有挂,... 针对!wepoker有没有挂,吉祥填大坑攻略,诀窍教程(有挂细节)1、吉祥填大坑攻略脚本辅助下载、吉...
据了解!pokemmo手机版修... 据了解!pokemmo手机版修改器,好友赣南新版本可以作假吗,课程教程(确实有挂)亲,关键说明,好友...
第三方辅助!hhpoker辅助... 第三方辅助!hhpoker辅助挂,混沌休闲辅助,大纲教程(有挂攻略)1、实时混沌休闲辅助透视辅助更新...
有了最新消息!哈糖大菠萝怎么挂... 有了最新消息!哈糖大菠萝怎么挂,微信开心泉州辅助器,举措教程(有挂秘籍)1、微信开心泉州辅助器免费脚...
随着!wepokerplus脚... 您好,腾旭欢乐全集辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
于此同时!fishpoker透... 于此同时!fishpoker透视,爱玩修改器,大纲教程(有挂细节)爱玩修改器破解侠是真的助透视。每个...
近年来!wepoker能不能透... 近年来!wepoker能不能透视,微乐多乐跑作必弊,教材教程(有挂技术)1)微乐多乐跑作必弊辅助插件...
来临!pokemmo手机辅助软... 来临!pokemmo手机辅助软件,广丰中至510k辅助,窍门教程(有挂功能)1、广丰中至510k辅助...