Controller的使用
注解 | 描述 |
---|---|
@Controller | 就处理http请求 |
@RestController | Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller |
@RequestMapping | 配置url映射 |
@RequestParam | 获取请求参数的值 |
@PathVariable | 获取url中的数据 |
@GetMapping | 组合注解 |
@RestController
public class HelloController {
//http://localhost:8081/demo/hello/100
@RequestMapping(value = "/hello" ,method = RequestMethod.GET)
private String sayHello(){
return "Hello String Boot";
}
//http://localhost:8081/demo/hello/100
@RequestMapping(value = "/hello1/{id}" ,method = RequestMethod.GET)
private String sayHello1(@PathVariable("id") Integer id){
return "id"+id;
}
//http://localhost:8081/demo/hello?id=100
@RequestMapping(value = "/hello2" ,method = RequestMethod.GET)
private String sayHello2(@RequestParam("id") Integer id){
return "id"+id;
}
}
SpringBoot配置文件
在resources下的application.properties
#端口号
server.port=8081
#root
server.context-path=/demo
访问http://localhost:8081/demo/hello就能够访问成功了