一、需求
需求:使用 SpringBoot 开发一个web应用,浏览器发起请求 /hello后,给浏览器返回字符串 "Hello World "。
二、步骤
- 创建springboot工程,并勾选web开发相关依赖。
- 定义HelloController类,添加方法 hello,并添加注解。
package com.test.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//请求处理类
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("Hello World");
return "Hello World";
}
}
- 运行测试
注意:默认端口为8080,但是有些情况会报错(o.s.b.d.LoggingFailureAnalysisReporter),因为端口被占用,解决方法就是:在resources目录下的application.properties中修改端口,输入server.port=8090。