SpringBoot之Controller的使用详解
作者:ren-zhe 时间:2023-02-16 21:18:30
本文介绍了 SpringBoot之Controller的使用,分享给大家,具体如下:
1.@Controller:处理http请求
2.@RestController:Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller
3.@RequestMapping 配置url映射
1.现在有一个需求(即可以使用localhost:8080/hello和localhost:8080/hi都可以访问):
@RestController
public class HelloController {
@RequestMapping(value={"/hello","hi"},method = RequestMethod.GET)//使用集合设置
public String say(){
return "Hello Spring Boot";
}
}
SpringBoot获取请求参数
1.@PathVariable–>获取url中的数据
2.@ReqeustParam–>获取请求参数的值,可以设置默认值以及是否必传
3.@GetMapping–>组合注解(相当于@RequestMapping同时限定请求方法为GET 方式)
1.第一种方式:
假如http://localhost:8080/hello为请求,springboot为需要传递的参数:http://localhost:8080/hello/spingboot,获取此种请求的参数的方式,使用@PathVariable注解
@RestController
public class HelloController {
@RequestMapping("/hello/{params}")//获取请求为http://localhost:8080/hello/XXX 类型的参数
public String hello(@PathVariable("params") String paramsStr) {//声明一个变量接收请求中的参数
return "parameter is "+paramsStr;
}
}
运行程序,输入http://localhost:8080/hello/spingboot进行测试:
2.第二种方式:
获取请求为http://localhost:8080/hello?params=spingboot类型的参数,使用@RequesParam注解,使用方法为@RequesParam("请求中的参数名params")
@RestController
public class HelloController {
//获取请求为http://localhost:8080/hello?xxx=xxx类型的参数
@RequestMapping("/hello")
public String hello(@RequestParam("params") String paramsStr) {//requestParam中的参数名称与请求中参数名称要一致
return "parameter is "+paramsStr;
}
}
如:@RequestParam(value="item_id",required=true) String id
@RequestParam中的其他属性:
--required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报错
--defaultValue:默认值,表示如果请求中没有同名参数时的默认值
启动程序,输入http://localhost:8080/hello?params=spingboot:
对于@RequestMapping(value="/hello",method = RequestMethod.GET)可以使用:@GetMapping(value="/hello"),如果是Post的话就是用@PostMapping
来源:http://blog.csdn.net/qq_35508033/article/details/71893371?utm_source=gold_browser_extension
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot开发RESTful接口与http协议状态表述
![](https://img.aspxhome.com/file/2023/4/84314_0s.png)
SpringMVC RESTFul实体类创建及环境搭建
![](https://img.aspxhome.com/file/2023/7/63297_0s.png)
C#11新特性使用案例详解
![](https://img.aspxhome.com/file/2023/2/80112_0s.png)
C#读写xml文件方法总结(超详细!)
关于@Value取值为NULL的解决方案
![](https://img.aspxhome.com/file/2023/1/113461_0s.png)
springboot后端配置多个数据源、Mysql数据库的便捷方法
利用C#实现绘制出地球旋转效果
![](https://img.aspxhome.com/file/2023/5/88315_0s.jpg)
Android自定义view实现滑动解锁九宫格控件
![](https://img.aspxhome.com/file/2023/2/98632_0s.gif)
Unity 修改FBX模型动画的操作
![](https://img.aspxhome.com/file/2023/3/80053_0s.jpg)
C语言实现贪吃蛇游戏演示
![](https://img.aspxhome.com/file/2023/1/103081_0s.gif)
springboot webflux 过滤器(使用RouterFunction实现)
Springboot webscoket自定义定时器
![](https://img.aspxhome.com/file/2023/7/109937_0s.png)
Android身份证号有效性校验工具类案例
Java实现简易俄罗斯方块
![](https://img.aspxhome.com/file/2023/1/67471_0s.png)
Java7到Java17之Switch语句进化史示例详解
c#读写App.config,ConfigurationManager.AppSettings 不生效的解决方法
![](https://img.aspxhome.com/file/2023/6/74816_0s.jpg)
Redisson延迟队列执行流程源码解析
![](https://img.aspxhome.com/file/2023/3/70353_0s.png)
C#正则表达式大全
Android Studio打包.so库到apk中实例详解
![](https://img.aspxhome.com/file/2023/4/113434_0s.png)
Java实现单例模式之饿汉式、懒汉式、枚举式
![](https://img.aspxhome.com/file/2023/6/110656_0s.png)