用到的注解
@SpringBootApplication 表示是Spring的启动类
@Controller 类注解
@RequestMapping("") 设置路由地址,路径不要有任何大写字符,因为windows系统对大小写不敏感但是linux系统是大小写敏感的,部署后可能会出现问题
@ResponseBody 方法的返回值作为数据返回,而不是返回一个静态页面(html、js等)
将下面这个类放在和 Spring 启动类相同的文件夹下(平级关系)或者子文件夹下
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@ResponseBody
@RequestMapping("/sayhi")
public String sayHi() {
return "Hello World";
}
}
打开浏览器输入127.0.0.1:8080/sayhi
结果:
知识点:
1:需要将对象托管到Spring Boot (Spring)的对象,必须要放在SpringBoot启动类的根路径或者是子路径中,否则不能被框架识别
2.SpringBoot 配置比较少,但是Spring Boot的设计思想是:约定大于配置