• spring boot 入参方式


    方式:

    1)、直接写,如public User index2(String name) 

    2)、@RequestParam

      与直接写的区别是,可以写默认值。

    3)、@RequestBody

       因为传入的是String类型的json,所以可以使用String类型,如:@RequestBody String jsonStr

      当然,也可以使用相应的对象类型。

    4)、@PathVariable

      url中{}

    例子:

    1)、直接写

    @RestController
    public class UserController {
        @GetMapping("/index")
        public User index(String name) {
            log.info("接收:{}",name);
            return new User(RandomUtil.randomInt(100),String.format("【入参=String】:%s", name));
        }
    }

    2)、@RequestParam

    @GetMapping("/index2")
    public User index2(@RequestParam(value="name",defaultValue="World") String name) {
        log.info("接收:{}",name);
        return new User(RandomUtil.randomInt(100),String.format("【入参=@RequestParam】:%s", name));
    }

    3)、@RequestBody--String

    @RequestMapping("/index3")
    public User index3(@RequestBody String jsonStr) {
        log.info("接收:{}",jsonStr);
        return new User(RandomUtil.randomInt(100),String.format("【入参=@RequestBody(String)】:%s", jsonStr));
    }

    3)、@RequestBody--对象

    @RequestMapping("/index4")
    public User index4(@RequestBody User user) {
        log.info("接收:{}",user);
        return new User(RandomUtil.randomInt(100),String.format("【入参=@RequestBody(对象)】:%s", user));
    }

    4)、@PathVariable

    @RequestMapping("/index5/{id}")
    public User index5(@PathVariable("id") long id) {
        log.info("接收:{}",id);
        return new User(RandomUtil.randomInt(100),String.format("【入参=@RequestBody】:%s", id));
    }

  • 相关阅读:
    Head First设计模式读书笔记
    通过FileReference打开本地图片崩溃的解决方法
    Asea——轻量级的AS3模块配置与加载管理库
    AGAL反编译器
    [Stage3D]硬件加速的径向模糊
    C#的timer类问题~!
    c语言中的大数运算模块
    TRACERT命令及用法
    Linux用户和用户组的管理概述
    linux下挂载windows的共享文件目录ftp文件夹到/root/wind目录
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/10348957.html
Copyright © 2020-2023  润新知