SpringBoot的Controller使用

		<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.效果

  

 

分类: SpringBoot
0
0
« 上一篇: SpringBoot属性配置
» 下一篇: Mysql介绍,与将脚本导入新数据库
	</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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值