Controller
1. HelloController
HelloController:一个最简单的Controller
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello world!";
}
}
2. 注解解析
2.1 @RestController
该注解主要由以下两个子注解构成。
@Controller
@ResponseBody
- @Controller:该注解继承自@Component注解,用于对象的实例化
- @ResponseBody:该注解将函数的返回值以JSON的形式返回给浏览器
2.2 @RequestMapping("/hello")
- 修饰于类:将类的方法置于新的命名空间,防止不同的类的函数接受请求冲突,符合模块化设计思想
- 修饰于方法:方法接收该指令,调用方法