Day04-10:Web入门-SpringBootWeb-快速入门
1.SpringBootWeb快速入门:
需求:使用SpiringBoot开发一个web应用,浏览器发起/hello请求后,给浏览器返回字符串“Hello World”。
-------------http://ocalhost:8080/hello--------------》
浏览器 服务器
《---------------------“Hello World”-------------------
1.创建springboot工程,并勾选web开发相关依赖。
2.定义HelloController,添加方法hello,并添加注解。
@RestController(表明是一个请求处理类)
public class HelloController{
RequestMapping("/hello")(要请求处理的/hello请求)
public class hello(){
System.out.println("Hello World");
return "Hello World";
}
}
由于Springboot版本太高了,JDK11发布的时候还没出来,所以把pom.xml文件里面的代码全部换成下面的
或者在创建的时候把springboot的版本调低一点
3.运行测试。
http://localhost:8080/hello
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- springboot父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--当前项目的坐标信息和一些描述信息-->
<groupId>com.itheima</groupId>
<artifactId>springboot-web-quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-web-quickstart</name>
<description>springboot-web-quickstart</description>
<!--选择的JDK版本:创建出来的时候是17,我改成11了-->
<properties>
<java.version>11</java.version>
</properties>
<!--添加了两个依赖-->
<dependencies>
<!--springboot进行web开发的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--进行单元测试的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--springboot的一个Maven插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>