芋道SpringBoot配置Maven、创建SpringBoot项目、创建Web接口、读取配置信息

🌹作者主页:青花锁 🌹简介:Java领域优质创作者🏆、Java微服务架构公号作者😄

🌹简历模板、学习资料、面试题库、技术互助

🌹文末获取联系方式 📝

在这里插入图片描述


系列文章目录

第一章 芋道 Spring Boot 快速入门



前言

芋道 SpringBoot是一款国产的SpringCloud微服务框架,包括Outh2.0、微服务网关、微服务注册中心、配置中心、消息队列、任务调度、链路追踪、服务保障等。

今天介绍下芋道 SpringBoot入门,包括SpringBoot介绍、第一个SpringBoot案例,所需要下载安装的基础工具等。


1、芋道网站

源码分析首页:https://www.iocoder.cn/
芋道 SpringBoot快速入门章:https://www.iocoder.cn/Spring-Boot/quick-start/?github

下面是我自己整理的Spingboot、微服务学习专栏,从Spingboot 零基础到微服务实战。
SpringBoot框架学习专栏:https://blog.csdn.net/s445320/category_12273537.html
SpringCloud微服务学习专栏 :https://blog.csdn.net/s445320/category_12439818.html
微服务实战专栏:https://blog.csdn.net/s445320/category_12345409.html


2、下载和安装maven工具

https://maven.apache.org/download.cgi
常用版本3:https://dlcdn.apache.org/maven/maven-3(只有最新的几个版本)
更新下载站:https://archive.apache.org/dist/maven/maven-3/ (包含从3.0 到 3.9的大多数版本)
下载解压,修改conf下的setting.xml,优先从阿里云镜像拉取关联包

<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    <mirror>
        <id>maven.net.cn</id>
        <name>oneof the central mirrors in china</name>
        <url>http://maven.net.cn/content/groups/public/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    <mirror>
        <id>central</id>
        <name>Maven Repository Switchboard</name>
        <url>http://repo1.maven.org/maven2/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

修改本地maven库路径

<localRepository>d:\localRepository</localRepository>

3、第一个SpringBoot 项目

3.1、初始化项目

https://start.spring.io/
在这里插入图片描述

3.2、配置maven

在这里插入图片描述

3.3、启动项目

在这里插入图片描述

在pom.xml添加web依赖,集成内嵌tomcat

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

在这里插入图片描述

3.4、定义一个hello world接口

3.4.1、在Controller包下创建类,并编写hello world接口

在这里插入图片描述

3.4.2、通过浏览器访问接口

地址:http://localhost:8080/hello

在这里插入图片描述

3.5、解决接口跨域问题

3.5.1、类或方法解决接口跨域问题

1、在class或method上加上【@CrossOrigin(“*”)】,解决跨域问题

在这里插入图片描述

3.5.2、配置全局跨域

新建【config】package,添加自定义的跨域java文件

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 MyWebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedMethods("*")
                .maxAge(1800)
                .allowedOrigins("*");
    }
}

4、接收参数

4.1、通过HttpServletRequest 接收参数

前端访问路径:http://127.0.0.1/user/login?userName=zs&pwd=123

后端Java代码:

	@RequestMapping("/login")
    public Map login(HttpServletRequest request , HttpServletResponse response) {
        // URL: http://127.0.0.1/user/login?userName=zs&pwd=123
        Map map = new HashMap();
        map.put("code" , 200);
        map.put("msg" , "success");

        //第一种方式接收入参
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("pwd");
        return map;
    }

4.2、通过@RequestParam接收参数

前端访问路径:http://127.0.0.1/user/login?userName=zs&pwd=123

后端Java代码:

    @RequestMapping("/login")
    public Map login(@RequestParam(value="name" ,required=true) String userName,
                     @RequestParam(value="pwd" ,required=true) String password,
                     HttpServletResponse response) {
        // @RequestParam(value="name" ,required=true) required=true传入的值不允许为空
        // URL: http://127.0.0.1/user/login?userName=zs&pwd=123
        //第二种方式接收入参
        System.out.println(userName + "||" + password);

        Map map = new HashMap();
        map.put("code" , 200);
        map.put("msg" , "success");
        return map;
    }

4.3、通过@PathVariable 接收参数

前端访问路径:http://127.0.0.1/user/logintwo/zs/123

后端Java代码:

    @RequestMapping("/logintwo/{userName}/{pwd}")
    public Map loginTwo(@PathVariable String userName,
                        @PathVariable String pwd) {
        // URL: http://127.0.0.1/user/logintwo/zs/123
        //第三种方式接收入参
        System.out.println(userName + "||" + pwd);

        Map map = new HashMap();
        map.put("code" , 200);
        map.put("msg" , "success");

        return map;
    }

4.4、通过@RequestBody 接收参数

前端JavaScript代码:

	//针对@RequestBody 接收入参的前端ajax请求
    $.ajax({
      url: "http://localhost:8089/api/Home",
      data: JSON.stringify(obj), 
      method: "post",
      dataType: "json",
      contentType: 'application/json',
      success: function (data) {
          console.log(data)
          if (data.code == 200) {
              alert("登录成功");
          } else {
              alert("登录失败:" + data.msg);
          }
      }

后端Java代码:

    @PostMapping("/register")
    public Map register(@RequestBody User user) {
        // URL: http://127.0.0.1/user/register/
        // {userName:xxx,pwd:xxx}
        //第四种方式接收入参
        System.out.println(new Gson().toJson(user) );

        Map map = new HashMap();
        map.put("code" , 200);
        map.put("msg" , "success");

        return map;
    }

5、starter机制

starter 中整合了该场景下各种可能用到的依赖,用户只需要在 Maven 中引入 starter 依赖,SpringBoot 就能自动扫描到要加载的信息并启动相应的默认配置
比如:

<!--spring boot parent项目依赖,版本依赖-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.13</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

在这里插入图片描述

Spring Boot 项目可以通过继承 spring-boot-starter-parent 来获得一些合理的默认配置,它主要提供了以下特性:

  • 默认 JDK 版本(Java 8)
  • 默认字符集(UTF-8)
  • 依赖管理功能
  • 资源过滤
  • 默认插件配置
  • 识别 application.properties 和 application.yml 类型的配置文件

6、YAML标记语言

springBoot 默认使用以下 2 种全局的配置文件,其文件名是固定的。

6.1、application.properties

server.port= 8081

6.2、application.yml

server:
 	port: 8081

6.2.1、YAML 简介

YAML 全称 YAML Ain’t Markup Language,比xml更适合做配置文件
YAML 的语法如下:

  • 使用缩进表示层级关系。
  • 缩进时不允许使用 Tab 键,只允许使用空格。
  • 缩进的空格数不重要,但同级元素必须左侧对齐。
  • 大小写敏感。

比如:

 server:
 	port: 8081
 spring:
 	profiles: dev
 	datasource:
 		url: xxxxxxxxx

6.2.2、YYAML 支持以下三种数据结构

6.2.2.1、对象:键值对的集合

使用缩进表示对象与属性的层级关系

 user:
    name: Kelvin
    gender:

行内写法:

 user: {name: Kelvin,gender:}
6.2.2.2、数组:一组按次序排列的值

使用-表示集合元素

 hobbyList:
	 - run
	 - badminton
	 - mountain climb

hobbyList: [run,badminton,mountain climb]

使用,表示数组元素

hobbies:run,badminton,mountain climb
6.2.2.3、字面量:单个的、不可拆分的值

数值、日期、字符串等
YAML组织结构:一个文件可有多个文档组成,文档之间用“—”分隔

7、配置文件与JavaBean绑定

SpringBoot 提供了以下 2 种方式进行配置绑定:

7.1、使用 @ConfigurationProperties 注解

定义JavaBean

user:
  userName: Kelvin
  gender:# 对应的Bean文件里的数组
  hobbies: run, badminton, mountainclimb
  # 对应的Bean文件里的List
  hobbyList:
    - run
    - badminton
    - mountainclimb
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
/**
 * 用户实体类
 */
@Data
@Component
@ConfigurationProperties(prefix = "user")
public class User {

    /**
     * 名字
     */
//    @Value("${user.userName}")
    private String userName;

    /**
     * 性别
     */
//    @Value("${user.gender}")
    private String gender;

    /**
     * 爱好
     */
//    @Value("${user.hobbies}")
    private String[] hobbies;

    /**
     * 爱好
     */
    private List<String> hobbyList;

    public User() {
    }
}
@SpringBootTest
class SpringbootApplicationTests {
    /**
     * 用户实体类对象
     */
    @Autowired
    private User user;

    @Test
    void contextLoads() {
        System.out.println("#############user: " + user);
    }
}

打印日志:#############user: User{name=‘Kelvin’, gender=‘男’, hobbies=[run, badminton, mountainclimb]}

7.2、使用 @Value 注解

/**
 * 用户实体类
 */
@Component
public class User {
    /**
     * 名字
     */
    @Value("${user.userName}")
    private String userName;
    /**
     * 性别
     */
    @Value("${user.gender}")
    private String gender;

    /**
     * 爱好
     */
    private String[] hobbies;
}

7.3、@Value 与 @ConfigurationProperties 对比

7.3.1、 使用位置不同

  • @ConfigurationProperties:标注在 JavaBean 的类名上;
  • @Value:标注在 JavaBean 的属性上。

7.3.2、功能不同

  • @ConfigurationProperties:用于批量绑定配置文件中的配置;
  • @Value:只能一个一个的指定需要绑定的配置。

7.3.3、松散绑定支持不同@ConfigurationProperties:支持松散绑定(松散语法),例如实体类 Person 中有一个属性为 firstName,那么配置文件中的属性名支持以下写法:

  • person.firstName
  • person.first-name
  • person.first_name
  • PERSON_FIRST_NAME
    @Vaule:不支持松散绑定。

7.3.4、 SpEL 支持不同

  • @ConfigurationProperties:不支持 SpEL 表达式;
  • @Value:支持 SpEL 表达式。

7.3.5、复杂类型封装

  • @ConfigurationProperties:支持所有类型数据的封装,例如 Map、List、Set、以及对象等;
  • @Value:只支持基本数据类型的封装,例如字符串、布尔值、整数等类型。

7.3.6、应用场景不同

@Value 和 @ConfigurationProperties 两个注解之间,并没有明显的优劣之分,它们只是适合的应用场景不同而已。

  • 若只是获取配置文件中的某项值,则推荐使用 @Value 注解;
  • 若专门编写了一个 JavaBean 来和配置文件进行映射,则建议使用 @ConfigurationProperties 注解。

7.4、非application配置文件

@PropertySource(value =“classpath:userinfo.properties”)
定义配置文件

user.userName=Tiger
user.gender=男
user.hobbies=run,badminton,mountainclimb

往期热门专栏回顾

专栏描述
Java项目实战介绍Java组件安装、使用;手写框架等
Aws服务器实战Aws Linux服务器上操作nginx、git、JDK、Vue
Java微服务实战Java 微服务实战,Spring Cloud Netflix套件、Spring Cloud Alibaba套件、Seata、gateway、shadingjdbc等实战操作
Java基础篇Java基础闲聊,已出HashMap、String、StringBuffer等源码分析,JVM分析,持续更新中
Springboot篇从创建Springboot项目,到加载数据库、静态资源、输出RestFul接口、跨越问题解决到统一返回、全局异常处理、Swagger文档
Spring MVC篇从创建Spring MVC项目,到加载数据库、静态资源、输出RestFul接口、跨越问题解决到统一返回
华为云服务器实战华为云Linux服务器上操作nginx、git、JDK、Vue等,以及使用宝塔运维操作添加Html网页、部署Springboot项目/Vue项目等
Java爬虫通过Java+Selenium+GoogleWebDriver 模拟真人网页操作爬取花瓣网图片、bing搜索图片等
Vue实战讲解Vue3的安装、环境配置,基本语法、循环语句、生命周期、路由设置、组件、axios交互、Element-ui的使用等
Spring讲解Spring(Bean)概念、IOC、AOP、集成jdbcTemplate/redis/事务等

资料获取,更多粉丝福利,关注下方公众号获取

在这里插入图片描述

  • 96
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 103
    评论
### 回答1: 好的,我来给你简要介绍一下如何使用 Spring BootMaven 来实现多客户端同步播放视频。 首先,你需要在你的项目中添加 Spring BootMaven 的依赖。在你的 pom.xml 文件中加入下面的内容: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 然后,你需要编写一个服务端程序来处理客户端的请求。在这个程序中,你需要使用 Spring Boot 的 @RestController 注解来标记这个类是一个 RESTful 服务端点,并使用 @GetMapping 注解来标记一个处理 GET 请求的方法。 例如: ``` @RestController public class VideoController { @GetMapping("/video/{id}") public String getVideo(@PathVariable String id) { // 这里是获取视频的代码 return "视频内容"; } } ``` 然后,你需要编写客户端程序来调用这个服务端点。在客户端中,你可以使用 HTTP 客户端库(例如 Apache HttpClient 或 OkHttp)来发送 HTTP 请求。 例如: ``` CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://localhost:8080/video/123"); CloseableHttpResponse response = httpClient.execute(httpGet); // 获取视频内容 InputStream inputStream = response.getEntity().getContent(); // 这里是播放视频的代码 ``` 希望这些信息能帮助你实现多客户 ### 回答2: 要实现多客户端同步播放视频,首先需要使用Spring BootMaven来构建项目和管理依赖。以下是一个基本的实现步骤: 1. 首先,在Maven的pom.xml文件中添加所需的依赖项,包括Spring Boot和其他必要的库、框架和工具。 2. 创建一个Spring Boot应用程序的入口类,并使用@SpringBootApplication注解进行标记。 3. 在应用程序配置文件(application.properties或application.yml)中,配置相关的属性,例如视频文件的位置、端口号等。 4. 创建一个视频播放控制器类,用于接收并处理客户端请求。可以使用@RestController注解在类上进行标记。 5. 在控制器类中,创建一个GET请求的接口,用于获取视频文件的信息,并返回给客户端。 6. 使用Java的I/O操作,读取视频文件并将其转换为字节流。 7. 在控制器类中,使用WebSocket协议来建立客户端之间的通信,以实现多客户端的同步播放。 8. 创建一个WebSocket处理类,用于处理和管理客户端之间的连接、消息和事件。 9. 在WebSocket处理类中,实现客户端之间的播放同步逻辑。可以使用计时器或其他方式来保持客户端之间的时间同步。 10. 在客户端的前端页面中,使用HTML5的<video>标签来播放视频,并通过JavaScript代码发送WebSocket消息来请求和接收服务器端的指令。 11. 运行Spring Boot应用程序,并访问相应的接口和页面来测试多客户端同步播放视频的功能。 这里只是一个大致的实现步骤,具体的细节和实现方式可以根据实际需求进行调整和拓展。希望对你有所帮助。 ### 回答3: 要实现Spring BootMaven实现多客户端同步播放视频,可以按照以下步骤进行: 1. 首先,在Spring Boot项目的pom.xml文件中添加相关的依赖项,如Spring Boot Web和Thymeleaf等。这些依赖项可以通过Maven自动下载和管理。 2. 创建一个Video类,用于保存视频的相关信息,例如视频的URL、标题、描述等。 3. 创建一个VideoController类,用于处理与视频相关的HTTP请求。可以使用@RestController注解来将该类标记为一个控制器,通过@RequestMapping注解来定义与视频相关的RESTful接口。 4. 在VideoController中,可以使用Java IO或Apache Commons IO等库从服务器或外部资源中读取视频文件,并将视频保存到合适的位置。 5. 创建一个HTML模板文件,包含一个可以播放视频的媒体元素,例如使用HTML5的<video>元素。 6. 在模板文件中使用Thymeleaf模板引擎来动态生成视频播放器的URL、标题、描述等内容。 7. 在VideoController中,用一个@GetMapping注解标记一个用于返回视频播放页的方法。在该方法中,可以通过Thymeleaf模板引擎加载之前创建的模板文件,并将Video对象作为模板的数据模型传递给模板。 8. 启动Spring Boot应用程序,通过访问指定的URL来播放视频。 9. 如果需要实现多客户端同步播放视频,可以考虑使用WebSocket或长轮询等技术。通过WebSocket,可以建立一个实时的双向通信通道,以便多个客户端之间进行视频播放的同步。当其中一个客户端执行播放、暂停或快进等操作时,可以通过WebSocket将该操作消息广播给其他客户端,从而实现同步播放效果。 10. 在VideoController中,可以使用Spring WebSocket支持来处理与视频同步播放相关的WebSocket请求和消息。 以上是使用Spring BootMaven实现多客户端同步播放视频的基本步骤和思路,具体实现的细节和代码编写需要根据具体需求和技术要求进行调整。
评论 103
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青花科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值