<div class="blogStats">
<div id="blog_stats">
隨筆- 628
文章- 0
評論- 25
</div><!--end: blogStats -->
</div><!--end: navigator 博客導航欄 -->
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>
</div><!--end: forFlow -->
</div>