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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring BootController之间的调用可以通过注入调用使用RestTemplate进行 HTTP 请求调用两种方式实现。 1. 注入调用:在Controller注入其他Controller类的实例,即可在当前Controller调用其他Controller的接口。注入方式一般通过@Autowired或@Resource注解实现。例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private OrderController orderController; @GetMapping("/{userId}/order") public String getUserOrder(@PathVariable Long userId) { String order = orderController.getOrder(userId); return "User: " + userId + ", Order: " + order; } } ``` 2. HTTP请求调用:在Controller使用RestTemplate实现HTTP请求调用其他Controller的接口。RestTemplate是Spring框架提供的基于HTTP协议的客户端工具,可以用来调用RESTful服务。例如: ```java @RestController @RequestMapping("/order") public class OrderController { @Autowired private RestTemplate restTemplate; @GetMapping("/{userId}") public String getOrder(@PathVariable Long userId) { String userUrl = "http://localhost:8080/user/" + userId; String user = restTemplate.getForObject(userUrl, String.class); return "Order for User " + user; } } ``` 需要注意的是,如果在同一个Spring Boot用程序使用注入调用需要确保被注入的Controller类被声明为bean,否则会出现NullPointerException异常;而使用HTTP请求调用可以直接调用其他Controller类的接口,无需担心依赖关系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值