需求:
使用Spring Boot开发一个web应用,浏览器发起/hello请求后,给浏览器返回字符串Hello World
步骤:
1.创建Spring Boot工程,并勾选开发相关依赖
勾选Web下Spring Web依赖
创建好工程会自动生成一个启动类,基本固定写法
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootGzist1Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootGzist1Application.class, args);
}
}
2.在java-->组织名下创建一个controller包定义HelloController类,添加Hello方法,并添加注解
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//注解为请求处理类
@RestController
public class HelloController {
//注解请求/hello时访问hello()方法
@RequestMapping("/hello")
public String hello(){
return "Hello World";//返回给浏览器
}
}
3.运行测试