@RequestParam和@RequestBody部分使用场景总结

总结代码如下

package com.woer.receipt_callback.controller;

import cn.hutool.log.StaticLog;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 总结:
 *  一、GetMapping 和 PostMapping 底层调用的都是:RequestMapping
 *
 *  二、后端接收【普通参数】无需注解:public MyUser one(String name, Integer age) 使用(Get、Post都能传参)
 *      2-1:Get使用 【localhost:9081/one?name=王五&age=61】 参数拼接
 *      2-2:POST使用【localhost:9081/two】 方法体中写入:{"name": "李白","age": 22}
 *
 *  三、后端接收【普通参数】使用注解 @RequestParam
 *          public MyUser five(@RequestParam(name = "name", required = false, defaultValue = "无名剑客") String name,
 *                             @RequestParam(name = "age", required = false, defaultValue = "1024") Integer age)
 *      3-1:Get使用 【localhost:9081/five?name=王五&age=61】 参数拼接
 *      3-2:POST使用【localhost:9081/five】 方法体中写入:{"name": "李白","age": 22}
 *
 *  四、后端接收【对象】使用注解 @RequestBody
 *      4-1:方法体 public MyUser seven(@RequestBody MyUser user){
 *      4-2:post传参Json字符串 {"name": "李白","age": 22}
 *
 *  五、在后端接收的方法里 @RequestBody可以和 @RequestParam同时使用,但一个方法里,@RequestBody只能有一个,@RequestParam可以有多个
 *
 * @author HeJinYang
 * @date 2023/10/13 22:41
 */
@RestController
public class RequestParamController {
    /**
     * get提交:localhost:9081/one?name=王五&age=61
     * 打印结果:MyUser(name=王五, age=61)
     */
    @GetMapping("/one")
    public MyUser one(String name, Integer age){
        MyUser user = new MyUser(name, age);
        StaticLog.info("user:{}", user);

        return user;
    }

    /**
     * post提交:localhost:9081/two {"name": "李白","age": 22}
     * 打印结果:MyUser(name=李白, age=22)
     */
    @PostMapping("/two")
    public MyUser two(String name, Integer age){
        MyUser user = new MyUser(name, age);
        StaticLog.info("user:{}", user);

        return user;
    }

    /**
     * get提交:localhost:9081/three?name=王五&age=61
     * 打印结果:No primary or single unique constructor found for class com.woer.receipt_callback.controller.tController$MyUser
     */
    @GetMapping("/three")
    public MyUser three(MyUser user){
        StaticLog.info("user:{}", user);

        return user;
    }

    /**
     * post提交:localhost:9081/four 请求体中 {"name": "李白","age": 22}
     * 打印结果:No primary or single unique constructor found for class com.woer.receipt_callback.controller.tController$MyUser
     */
    @PostMapping("/four")
    public MyUser four(MyUser user){
        StaticLog.info("user:{}", user);

        return user;
    }

    /**
     * get提交: localhost:9081/three?name=王五&age=61
     * post提交:localhost:9081/five 请求体中 {"name": "李白","age": 22}
     * 打印结果:No primary or single unique constructor found for class com.woer.receipt_callback.controller.tController$MyUser
     */
    @RequestMapping("/five")
    public MyUser five(@RequestParam(name = "name", required = false, defaultValue = "无名剑客") String name,
                       @RequestParam(name = "age", required = false, defaultValue = "1024") Integer age){
        MyUser user = new MyUser(name, age);
        StaticLog.info("user:{}", user);
        return user;
    }

    /**
     * get提交:localhost:9081/six?name=王五&age=61
     * post提交:请求体中 {"name": "李白","age": 22}
     * 打印结果:No primary or single unique constructor found for class com.woer.receipt_callback.controller.RequestParamController$MyUser
     */
    @RequestMapping("/six")
    public MyUser six(@RequestParam("user") MyUser user){
        StaticLog.info("user:{}", user);

        return user;
    }

    /**
     * post提交:localhost:9081/seven
     * post提交:json {"name": "李白","age": 22}
     * 打印结果:
     */
    @RequestMapping("/seven")
    public MyUser seven(@RequestBody MyUser user){
        StaticLog.info("user:{}", user);

        return user;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class MyUser{
        public String name;
        public Integer age;
    }
}


你可能感兴趣的:(#,JavaEE(SSM框架),SpringMVC,java)