上篇我们已经创建了一个Spring Boot项目,然后我们可以暂时用不到的文件删除,剩如图所示
在创建项目名的同级目录下创建一个controller类编写一个helloworld程序
package com.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/hello")
public class controller {
@GetMapping("/hello")
@ResponseBody
public String hello(){
return "hello023";
}
}
在main方法中点击【run】
等待一会如果有以下信息,说明运行成功
运行成功后,浏览器访问 “http://localhost:8080/hello
” 地址。
这样第一个SpringBoot的helloworld程序就运行成功了!!
注:在创建项目时如果没有勾选spring-web依赖模块,则我们可以在pom.xml中添加
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--starter-web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
修改 properties 配置文件实例:
打开 resources 下的 application.properties
#配置端口和访问时的项目名
server.port=8081
server.servlet.context-path=/demo