SpringBoot的Controller使用
一:
1.注解

2.control注解



3.效果

4.RespomseBody
1 package com.caojun.springboot;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.beans.factory.annotation.Value;
5 import org.springframework.stereotype.Controller;
6 import org.springframework.web.bind.annotation.RequestMapping;
7 import org.springframework.web.bind.annotation.RequestMethod;
8 import org.springframework.web.bind.annotation.ResponseBody;
9 import org.springframework.web.bind.annotation.RestController;
10
11 @Controller
12 @ResponseBody
13 public class HelloSpringBoot {
14
15 @Autowired
16 private PeoplePerties peoplePerties;
17
18 @RequestMapping(value="/hello")
19 public String say(){
20 return peoplePerties.getName()+"====="+peoplePerties.getAge();
21 // return "index";
22 }
23 }
5.效果

6.hello与hi都可以访问
1 @RestController
2 public class HelloSpringBoot {
3
4 @Autowired
5 private PeoplePerties peoplePerties;
6
7 @RequestMapping(value={"/hello","/hi"})
8 public String say(){
9 return peoplePerties.getName()+"====="+peoplePerties.getAge();
10 // return "index";
11 }
12 }
7.效果

8.RequestMapping的类上使用的方式
1 @RestController
2 @RequestMapping(value = "/hello")
3 public class HelloSpringBoot {
4
5 @Autowired
6 private PeoplePerties peoplePerties;
7
8 @RequestMapping(value={"/say"})
9 public String say(){
10 return peoplePerties.getName()+"====="+peoplePerties.getAge();
11 // return "index";
12 }
13 }
9.效果

二:
1.注解

2.PathVariable的使用
1 @RestController
2 @RequestMapping(value = "/hello")
3 public class HelloSpringBoot {
4
5 @Autowired
6 private PeoplePerties peoplePerties;
7
8 @RequestMapping(value={"/say/{id}"})
9 public String say(@PathVariable("id") Integer id){
10 return "id:"+id;
11 // return peoplePerties.getName()+"====="+peoplePerties.getAge();
12 // return "index";
13 }
14 }
3.效果
看起来url特别简洁。

4.RequestParam的使用
这个针对的是?=这种url
1 @RestController
2 @RequestMapping(value = "/hello")
3 public class HelloSpringBoot {
4
5 @Autowired
6 private PeoplePerties peoplePerties;
7
8 @RequestMapping(value={"/say"})
9 public String say(@RequestParam("id") Integer myId){
10 return "id:"+myId;
11 // return peoplePerties.getName()+"====="+peoplePerties.getAge();
12 // return "index";
13 }
14 }
5.效果

6.设置默认值
1 @RestController
2 @RequestMapping(value = "/hello")
3 public class HelloSpringBoot {
4
5 @Autowired
6 private PeoplePerties peoplePerties;
7
8 @RequestMapping(value={"/say"})
9 public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){
10 return "id:"+myId;
11 // return peoplePerties.getName()+"====="+peoplePerties.getAge();
12 // return "index";
13 }
14 }
7.效果

8.GetMapping的使用
简化RequestMapping
1 @RestController
2 @RequestMapping(value = "/hello")
3 public class HelloSpringBoot {
4
5 @Autowired
6 private PeoplePerties peoplePerties;
7
8 // @RequestMapping(value={"/say"},method = RequestMethod.GET)
9 @GetMapping(value = "/say")
10 public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){
11 return "id:"+myId;
12 // return peoplePerties.getName()+"====="+peoplePerties.getAge();
13 // return "index";
14 }
15 }
9.效果

</div>
<div class="postDesc">posted @ <span id="post-date">2017-10-29 21:24</span> <a href="https://www.cnblogs.com/juncaoit/">曹军</a> 阅读(<span id="post_view_count">11660</span>) 评论(<span id="post_comment_count">0</span>) <a href="https://i.cnblogs.com/EditPosts.aspx?postid=7751340" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(7751340);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=305870,cb_entryId=7751340,cb_blogApp=currentBlogApp,cb_blogUserGuid='b0df91bd-757a-e611-9fc1-ac853d9f53cc',cb_entryCreatedDate='2017/10/29 21:24:00';loadViewCount(cb_entryId);var cb_postType=1;</script>
本文深入讲解了SpringBoot中Controller的使用方法,包括注解、路径参数、请求参数、默认值设置及GetMapping简化等关键特性,适合初学者和进阶开发者参考。




934

被折叠的 条评论
为什么被折叠?



