目录

SPring-学习积累1-关于下载相关jdk-maven-版本

SPring 学习积累1 关于下载相关jdk maven 版本

3.15.1 注意下载的版本

有些是不适配的,官网有提示;

3.15.2 注意配置环境变量时需要注意admistartor 中的java路径和系统变量是否一致,一行要一致,不然后续安装maven之后,使用命令 mvn -version时会显示以下错误:

C:\Users\Lenovo>mvn -v

The JAVA_HOME environment variable is not defined correctly, this environment variable is needed to run this program.

 C:\Users\Lenovo>mvn -v 

The JAVA_HOME environment variable is not defined correctly, this environment variable is needed to run this program.

https://i-blog.csdnimg.cn/direct/89575b6b8b3a47cf903fcdfdb3d624d4.png

要学习spring就需要开始看官网相关要求:

1.有Intellij IDEA 或者VS Code 软件

2.JDK版本要求在17~21

3.14.1 Spring官网学习地址

https://i-blog.csdnimg.cn/direct/fa907086a7f14eb6b1bacee7a3f134b7.png

3.14.1 更据官网地址,下载demo用idea打开后,继 续编写DemoApplication.java代码:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
		
	}
	@GetMapping("/hello")
	public String hello (@RequestParam(value="name",defaultValue="world")String name) {
		return String.format("Hello, %s!", name);
		//return "Hello World!";
	}


}

3.14.2 代码解释

The @RestController annotation tells Spring that this code describes an endpoint that should be made available over the web. The @GetMapping(“/hello”) tells Spring to use our hello() method to answer requests that get sent to the http://localhost:8080/hello address. Finally, the @RequestParam is telling Spring to expect a name value in the request, but if it’s not there, it will use the word “World” by default.

对于上述代码,上述是原文解释:

1.@RestController

@RestController 是一个组合注解,用于标记一个类为 Spring MVC 的控制器,并且表明这个控制器返回的是数据(通常是 JSON 或 XML 格式),而不是视图页面。它结合了

@Controller 和 @ResponseBody 的功能,使得类中的方法可以直接返回数据,而无需经过视图解析器处理。

作用:

声明一个类为控制器。

表明该控制器返回的数据是直接发送给客户端的(而不是跳转到某个页面)。

@GetMapping 注解

原文:

“The @GetMapping(“/hello”) tells Spring to use our hello() method to answer requests that get sent to the http://localhost:8080/hello address.”

解释:

@GetMapping 是一个路由注解,用于定义一个 HTTP GET 请求的处理方法。它指定了一个特定的 URL 路径(在这个例子中是 /hello),并告诉 Spring 当有请求访问 http://localhost:8080/hello 时,应该调用 hello() 方法来处理这个请求。

作用:

将 HTTP GET 请求映射到指定的方法上。

定义请求的路径(URL)。

3. @RequestParam 注解

原文:

“The @RequestParam is telling Spring to expect a name value in the request, but if it’s not there, it will use the word ‘World’ by default.”

解释

@RequestParam 是一个参数绑定注解,用于从 HTTP 请求中提取参数值。在这个例子中,它告诉 Spring 从请求中查找一个名为 name 的参数。如果请求中没有提供 name 参数,它将使用默认值 "World"

  • 作用

    • 从请求中提取参数。
    • 提供默认值,以便在参数缺失时使用。

3. @RequestParam 注解

原文

“The @RequestParam is telling Spring to expect a name value in the request, but if it’s not there, it will use the word ‘World’ by default.”

解释

@RequestParam 是一个参数绑定注解,用于从 HTTP 请求中提取参数值。在这个例子中,它告诉 Spring 从请求中查找一个名为 name 的参数。如果请求中没有提供 name 参数,它将使用默认值 “World”。

作用:

从请求中提取参数。

提供默认值,以便在参数缺失时使用

上述代码总结:

Spring Boot 中的注解来定义一个简单的 RESTful API 接口:

@RestController 定义了一个控制器,返回数据而不是视图。

@GetMapping 定义了请求路径和请求方法(GET)。

@RequestParam 用于从请求中提取参数,并提供默认值。

完成上述之后就可以运行,运行之后在浏览器端搜索

3.14.3 (默认参数输出)

运行结果:

https://i-blog.csdnimg.cn/direct/3831d84a857749218cf5de51dd70e905.png

显示:

https://i-blog.csdnimg.cn/direct/2cd5404fd299411bbf31aa2b5fc75e7d.png

3.14.4 使用请求参数

给个参数,发现可以显示在客户端,你可以多次尝试不同的参数,localhost:8080/hello?name=XXX,可以自己定义“XXX"部分。

https://i-blog.csdnimg.cn/direct/a38f267b944e48749b8b322cc10f74a6.png

https://i-blog.csdnimg.cn/direct/771540abb498443aabfbd121c5e05834.png

ok,成功迈出第一步!!!!