zoukankan      html  css  js  c++  java
  • SpringBoot实现前后端数据交互、json数据交互、Controller接收参数的几种常用方式

    1.获取参数的集中常见注解

    • @PathVariable:一般我们使用URI template样式映射使用,即url/{param}这种形式,也就是一般我们使用的GET,DELETE,PUT方法会使用到的,我们可以获取URL后所跟的参数。
    • @RequestParam:一般我们使用该注解来获取多个参数,在()内写入需要获取参数的参数名即可,一般在PUT,POST中比较常用。
    • @RequestBody:该注解和@RequestParam殊途同归,我们使用该注解将所有参数转换,在代码部分在一个个取出来,也是目前我使用到最多的注解来获取参数

    2.获取请求路径参数

    1. get请求,url路径传参
      get请求一般通过url传参,如:

    http://localhost:8080/piano/add?brand="xinde" & price = "1200"
    后端要获取这些参数,可以使用@RequestParam注解

    
    @RestController
    public class HelloController {
        @RequestMapping(value="/hello",method= RequestMethod.GET)
        public String sayHello(@RequestParam Integer id){
            return "id:"+id;
        }
    
    
    1. get请求,url路径参数

    http://localhost:8080/piano/xinde/buyaoq/1200

    后端可以使用@PathVariable接收路径参数

    @RestController
    public class HelloController {
        @RequestMapping(value="/piano/{brand}/{price}",method= RequestMethod.GET)
        public String sayHello(@PathVariable("price") Integer id,@PathVariable("name") String name){
            return "id:"+id+" name:"+name;
        }
    }
    
  • 相关阅读:
    ubuntu qtcreator 硬件权限问题
    关于LuCi
    npm 使用记录
    ubuntu 下简单录音
    qthread 使用 signal 方法通信
    线程安全笔记一则
    ubuntu 设置 NAT 转发
    debian 中新建或调整 swap 空间
    关于 htonl 和 ntohl 的实现
    shell 调试手段总结
  • 原文地址:https://www.cnblogs.com/charlottepl/p/12564755.html
Copyright © 2011-2022 走看看