我的第一个SpringBoot应用

来源:互联网 发布:红叶知弦调教 编辑:程序博客网 时间:2024/05/18 13:04

Controller的使用

注解 描述 @Controller 就处理http请求 @RestController Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller @RequestMapping 配置url映射 @RequestParam 获取请求参数的值 @PathVariable 获取url中的数据 @GetMapping 组合注解
@RestControllerpublic 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#rootserver.context-path=/demo

访问http://localhost:8081/demo/hello就能够访问成功了
这里写图片描述

原创粉丝点击