• @RequestParam和@PathVariable的用法与区别


    package com.cherish.controller;
    
    import com.cherish.commonutils.ResultData;
    import org.springframework.web.bind.annotation.*;
    
    /**
     * pathVariable参数需要拼接到路径中,不需要判空
     * (@PathVariable String id,@PathVariable String name)
     *
     * 地址栏访问地址 :http://localhost:9090/get/pathVariable/520/cherish
     *
     * requestParam不要要拼接到路径中 @RequestParam("id") String id,@RequestParam("name"
     *  地址栏访问地址:  http://localhost:9090/get/requestParam?id=520&name=cherish
     *
     *  1、当URL指向的是某一具体业务资源(或资源列表),例如博客,用户时,使用@PathVariable
     *
     * 2、当URL需要对资源或者资源列表进行过滤,筛选时,用@RequestParam
     */
    @RestController
    @RequestMapping("/get")
    public class TestRequestController {
    
        @GetMapping("/requestParam")
        public ResultData getParam(@RequestParam("id") String id,@RequestParam("name") String name){
    
            return ResultData.ok().data("id",id).data("name",name);
        }
    
        @GetMapping("pathVariable/{id}/{name}")
        public ResultData getPath(@PathVariable String id,@PathVariable String name){
            return ResultData.ok().data("id",id).data("name",name);
        }
    }
    
    
    努力学习java的Cherish
  • 相关阅读:
    【uTenux实验】事件标志
    【uTenux实验】信号量
    【uTenux实验】任务管理
    【uTenux实验】写在开始实验之前
    Git撤销add、commit
    vim笔记
    Git使用方法(精心整理,绝对够用)
    git笔记三
    git笔记记录
    git笔记
  • 原文地址:https://www.cnblogs.com/cherish-code/p/14722969.html
Copyright © 2020-2023  润新知