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进行测试:

SpringBoot之Controller的使用详解

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:

SpringBoot之Controller的使用详解

对于@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

标签:SpringBoot,Controller
0
投稿

猜你喜欢

  • Spring Boot开发RESTful接口与http协议状态表述

    2023-05-27 03:26:48
  • SpringMVC RESTFul实体类创建及环境搭建

    2022-12-25 18:23:06
  • C#11新特性使用案例详解

    2023-11-26 03:19:15
  • C#读写xml文件方法总结(超详细!)

    2023-11-23 13:16:40
  • 关于@Value取值为NULL的解决方案

    2021-07-15 06:38:42
  • springboot后端配置多个数据源、Mysql数据库的便捷方法

    2022-05-01 07:21:37
  • 利用C#实现绘制出地球旋转效果

    2022-10-01 14:21:27
  • Android自定义view实现滑动解锁九宫格控件

    2021-12-28 02:24:16
  • Unity 修改FBX模型动画的操作

    2023-03-17 17:59:39
  • C语言实现贪吃蛇游戏演示

    2023-07-03 14:16:43
  • springboot webflux 过滤器(使用RouterFunction实现)

    2022-12-12 21:28:44
  • Springboot webscoket自定义定时器

    2023-02-12 05:34:02
  • Android身份证号有效性校验工具类案例

    2022-02-02 21:07:01
  • Java实现简易俄罗斯方块

    2022-12-18 14:07:58
  • Java7到Java17之Switch语句进化史示例详解

    2021-11-03 18:47:37
  • c#读写App.config,ConfigurationManager.AppSettings 不生效的解决方法

    2021-10-07 22:34:42
  • Redisson延迟队列执行流程源码解析

    2021-12-07 12:03:46
  • C#正则表达式大全

    2023-12-04 13:06:37
  • Android Studio打包.so库到apk中实例详解

    2022-06-25 19:19:25
  • Java实现单例模式之饿汉式、懒汉式、枚举式

    2022-12-17 17:50:13
  • asp之家 软件编程 m.aspxhome.com