zoukankan      html  css  js  c++  java
  • SpringBoot接收前端参数的三种方法

    都是以前的笔记了,有时间就整理出来了,SpringBoot接收前端参数的三种方法,首先第一种代码:

    @RestController
    public class ControllerTest {
     //访问路径:http://localhost:8080/hello/5
        @GetMapping(value = "/hello/{id}")
        public String hello(@PathVariable("id") Integer id){
            return "ID:" + id;
        }
    }

    第二种:

      可以通过注解@RequestParam("xxx")来绑定指定的参数,在这里声明的参数为ids,但是@RequestParam()注解中确实id,这个时候前端传的值就应该为id,如果前台传送的是ids的话,在这里是接收不到的。

    @RestController
    public class ControllerTest {
     //访问路径:http://localhost:8080/hello?id=1551
        @GetMapping(value = "/hello")
        public String hello(@RequestParam("id") Integer ids){
            return "ID:" + id;
        }
    }

    第三种像这种,如果传进来的参数为空的话,可以设置为默认的:

        //访问路径:http://localhost:8080/hello?id=1551
        @GetMapping(value = "/hello")   
        public String hello(@RequestParam(value = "id", required = false, defaultValue = "5") Integer id){       
               return "ID:" + id;
        }
  • 相关阅读:
    目录
    mysql 常用命令复习
    mysql备份及pymysql
    mysql 表操作
    mysql 库操作
    mysql 数据库
    各种推导式
    python 之I/O模型
    python 之进程
    python之 多线程(二)
  • 原文地址:https://www.cnblogs.com/itiande/p/9505227.html
Copyright © 2011-2022 走看看