@ApiParam @RequestParam @PathVariable 用法


文章來源:https://www.cnblogs.com/hello-tl/p/9204279.html 

1.@ApiParam ,是注解api的參數 ,也就是用於swagger提供開發者文檔 ,文檔中生成的注釋內容 。

2.@RequestParam , 是獲取前端傳遞給后端的參數,可以是get方式,也可以是post方式。其中如果前端傳遞的參數和后端你接受的參數起的名字字段是一致的可以省略不寫,也可以直接寫@RequestParam String title,如果不一致一定要完整寫,

3.@PathVariable , 獲取url后面參數,進行參數綁定

package com.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(value="apiTest", description="apiTest 控制器")
@RequestMapping("/apiTest")
public class ApiTestController {
    // @ApiOperation    用於方法上
    // @ApiParam        用於方法,參數,字段說明,表示對參數的添加元數據(說明或是否必填等)

   // 1.@ApiParam   是注解api的參數 ,也就是用於swagger提供開發者文檔 ,文檔中生成的注釋內容 。
    // 接受 POST 或者 GET 參數
    // @ApiOperation(value = "api說明", notes = "接口發布說明", httpMethod = "請求方式 [ POST | GET ]")
    @ApiOperation(value = "測試ApiOperation" , notes = "測試ApiOperation", httpMethod = "GET")
    // @RequestMapping(value = "URL地址" , method = "傳輸方式 [ RequestMethod.GET | RequestMethod.POST] ")
    @RequestMapping(value = "/ApiTest1" , method = RequestMethod.GET)
    public String ApiTest1(
            // @ApiParam ( name = "參數名稱" , value = "api描述" , required = 是否必傳[ true(必傳) 接收的值 | false(非傳) 默認等於 null ] ) 類型 參數綁定
            @ApiParam(name = "ApiParam",value="測試ApiParam",required = true) String ApiParam){
        /**
         * 訪問 localhost:8080/apiTest/ApiTest1
         * 頁面打印是空的並沒有報錯說 缺少ApiParam參數 這是因為 ---- ApiParam 里面 required 參數是配合 swagger-ui 用的
         *
         * 下伙子不要沖動---這是后可以訪問一下 localhost:8080/apiTest/ApiTest2 試試
         */
        return ApiParam;
    }

   // 2.@RequestParam,是獲取前端傳遞給后端的參數,可以是get方式,也可以是post方式。其中如果前端傳遞的參數和后端你接受的參數起的名字字段是一致的可以省略不寫,也可以直接寫@RequestParam String title,如果不一致一定要完整寫
    // 接受 POST 或者 GET 參數
    @ApiOperation(value = "測試ApiOperation" , notes = "測試ApiOperation", httpMethod = "GET")
    @RequestMapping(value = "/ApiTest2" , method = RequestMethod.GET)
    public String ApiTest2(
            // @RequestParam(name = "參數名稱",required = "是否必傳[ true(必傳) | false(非傳) ] 默認 true", defaultValue = "默認值") 類型 參數綁定
            @ApiParam(name = "ApiParam",value="測試ApiParam",required = true)
            @RequestParam(name = "ApiParam",required = true) String ApiParam){
        /**
         * @RequestParam(name = "ApiParam",required = false, defaultValue = "ApiParam 默認值") String ApiParam
         * 訪問 localhost:8080/apiTest/ApiTest2
         *      會返回 ApiParam 默認值
         *
         * @RequestParam(name = "ApiParam",required = true) String ApiParam
         * 訪問 localhost:8080/apiTest/ApiTest2
         *      會報錯 字符串參數 ApiParam 不存在
         *
         * 訪問 localhost:8080/apiTest/ApiTest2?ApiParam=測試地址
         *      會返回 測試地址
         */
        return ApiParam;
    }

    // 3.獲取url后面參數,進行參數綁定
    @ApiOperation(value = "測試ApiOperation" , notes = "測試ApiOperation", httpMethod = "GET")
    @RequestMapping(value = "/ApiTest3/{ApiParam}" , method = RequestMethod.GET)
    public String ApiTest3(
            @ApiParam(name = "ApiParam",value="測試ApiParam",required = true) @PathVariable String ApiParam){
        /**
         * 訪問 localhost:8080/apiTest/ApiTest3
         *      出返回 404
         *
         * 訪問 localhost:8080/apiTest/ApiTest3/測試地址
         *      出返回 測試地址
         */
        return ApiParam;
    }
}

文章來源:https://www.cnblogs.com/hello-tl/p/9204279.html 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM