访问网页的方式输出语句:
步骤:
- 添加springboot起步依赖
新建一个Maven项目,选择检查jdk1.8的版本 ,命名
2.创建成功后检查本地仓库是否有东西在下载,在pom.xml中添加
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.4.1</version>
</parent>
<!-- 添加web模块支持-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
之后点下刷新,加载依赖。
3.在Java中新建一个类:com.softeem.controller.UserController
4.在UserController中新建一个方法,public String hello
在顶部加@RestController,另外在方法上加 @RequestMapping(“hello”)
package com.softeem.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@RequestMapping("hello")
public String hello(){
return "hello world";
}
}
5.在com.softeem包下面新建一个测试类App,在类上面加@SpringBootApplication
package com.softeem;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}
6.启动类,在网页上输入http://localhost:8080/hello,就可以在网页上访问到打印输出的语句。
注:项目结构如图