SpringBoot的Controller使用
一:
1.注解

2.control注解



3.效果

4.RespomseBody
package com.caojun.springboot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@Controller
@ResponseBody
public class HelloSpringBoot {
@Autowired
private PeoplePerties peoplePerties;
@RequestMapping(value="/hello")
public String say(){
return peoplePerties.getName()+"====="+peoplePerties.getAge();
// return "index";
}
}
5.效果

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

8.RequestMapping的类上使用的方式
@RestController
@RequestMapping(value = "/hello")
public class HelloSpringBoot {
@Autowired
private PeoplePerties peoplePerties;
@RequestMapping(value={"/say"})
public String say(){
return peoplePerties.getName()+"====="+peoplePerties.getAge();
// return "index";
}
}
9.效果

二:
1.注解

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

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

6.设置默认值
@RestController
@RequestMapping(value = "/hello")
public class HelloSpringBoot {
@Autowired
private PeoplePerties peoplePerties;
@RequestMapping(value={"/say"})
public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){
return "id:"+myId;
// return peoplePerties.getName()+"====="+peoplePerties.getAge();
// return "index";
}
}
7.效果

8.GetMapping的使用
简化RequestMapping
@RestController
@RequestMapping(value = "/hello")
public class HelloSpringBoot {
@Autowired
private PeoplePerties peoplePerties;
// @RequestMapping(value={"/say"},method = RequestMethod.GET)
@GetMapping(value = "/say")
public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer myId){
return "id:"+myId;
// return peoplePerties.getName()+"====="+peoplePerties.getAge();
// return "index";
}
}
9.效果

SpringBoot的Controller使用的更多相关文章
- 【异常处理】Springboot对Controller层方法进行统一异常处理
Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...
- [技术博客] Springboot的Controller类使用
Springboot的Controller类使用 @Controller:处理http请求. 代码: @Controller public class QuestionController { ... ...
- Springboot对Controller层方法进行统一异常处理
Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...
- springboot中Controller没有被扫描
今天给客户开发登陆的密码加密需求,研究一下想,需要在本地搭一套环境,前台用js实现RAS加密,后台使用java解密.本是一套非常简单的环境,看最近springboot比较常用,所以想要搭一下sprin ...
- SpringBoot测试Controller层
一.准备工作 1.导入测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- springboot用controller跳转html页面
之前SSM框架,里面有webapps文件夹,用来存放前端页面和各种前端资源,现在SpringBoot中没有webapps文件夹,springboot结构如下: 第一.resourses下文件夹publ ...
- Q1:spring-boot中Controller路径无法被访问的问题
在学习spring-boot入门的第一个例子就是spring-boot-web的一个在页面上输出hello-world的例子,在运行这个例子的时候我遇到了下面这个简单的问题,但是第一次解决还是花了我很 ...
- 利用IDEA构建springboot应用--controller例子
微服务 微服务是一个新兴的软件架构,就是把一个大型的单个应用程序和服务拆分为数十个的支持微服务.一个微服务的策略可以让工作变得更为简便,它可扩展单个组件而不是整个的应用程序堆栈,从而满足服务等级协议. ...
- 小记SpringMVC与SpringBoot 的controller的返回json数据的不同
近期由于项目的改动变更,在使用springmvc和springboot测试的时候发现一个有趣的现象 1.springmvc的controller使用@ResponseBody返回的仅仅是json格式的 ...
随机推荐
- codeforces906 D
题目链接:http://codeforces.com/contest/906/problem/D 题意: 给你n个数,再给你l~r,求%m 题解: 一开始不会 后来查到了欧拉降幂定理: 然后就会了 这 ...
- 【转】I²C总线上拉电阻阻值如何选择?
I2C总线为何需要上拉电阻? I2C(Inter-Intergrated Circuit)总线是微电子通信控制领域中常用的一种总线标准,具有接线少,控制方式简单,通信速率高等优点. I2C总线的内部结 ...
- 【转】单片机系统中数字滤波的算法【C程序整理】
随机误差是有随机干搅引起的,其特点是在相同条件下测量同一个量时,其大小和符号做无规则变化而无法预测,但多次测量结果符合统计规律.为克服随机干搅引入的误差,硬件上可采用滤波技术,软件上可以采用软件算法实 ...
- POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离)
POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离) Description You have just moved from a ...
- 【UVA1401】Remember the Word Trie+dp
题目大意:给定一个字符串和一个字符串集合,问从集合中选出若干个串组成给定母串的不同方案数. 题解:有些类似于背包问题.状态很好表示,为:\(dp[i]\) 表示母串前 i 个字符的不同方案数,因此,有 ...
- 遭遇:“传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确” 错误
http://www.cnblogs.com/delphinet/archive/2010/03/09/1681777.html 正在写一个类似文章的发表系统.其中记录文章内容的字段Contents设 ...
- IO之间的比较
在高性能的IO体系设计中,有几个名词概念常常会使我们感到迷惑不解.具体如下: 序号 问题 1 什么是同步? 2 什么是异步? 3 什么是阻塞? 4 什么是非阻塞? 5 什么是同步阻塞? 6 什么是同步 ...
- Java体验的重点难点-----总结
1.关于JVM原理:http://www.cnblogs.com/zhanglei93/p/6590609.html 2.关于垃圾收集机制:http://www.cnblogs.com/zhangle ...
- Google Email 帐户泄露
最初爆出来的网站是:https://forum.btcsec.com/index.php?/topic/9426-gmail-meniai-parol/,是一个俄罗斯论坛,然后..就流传开来了... ...
- bzoj千题计划203:bzoj3994: [SDOI2015]约数个数和
http://www.lydsy.com/JudgeOnline/problem.php?id=3994 设d(x)为x的约数个数,给定N.M,求 用到的一个结论: 证明: 枚举n的约数i,枚举m的约 ...