<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                [TOC] # [@ApiImplicitParams、ApiImplicitParam的使用](https://www.cnblogs.com/h-c-g/p/11004020.html) ## @ApiImplicitParam: 作用在方法上,表示單獨的請求參數? 參數:? 1\. name :參數名。? 2\. value : 參數的具體意義,作用。? 3\. required : 參數是否必填。? 4\. dataType :參數的數據類型。? 5\. paramType :查詢參數類型,這里有幾種形式: |類型| 作用 | |--|--| path |以地址的形式提交數據 query| 直接跟參數完成自動映射賦值 body |以流的形式提交 僅支持POST header| 參數在request headers 里邊提交 form |以form表單的形式提交 僅支持POST 在這里我被坑過一次:當我發POST請求的時候,當時接受的整個參數,不論我用body還是query,后臺都會報Body Missing錯誤。這個參數和SpringMvc中的@RequestBody沖突,索性我就去掉了paramType,對接口測試并沒有影響。 ## @ApiImplicitParams: 用于方法,包含多個 @ApiImplicitParam:? 例: ``` @ApiOperation("查詢測試") @GetMapping("select") //@ApiImplicitParam(name="name",value="用戶名",dataType="String", paramType = "query") @ApiImplicitParams({ @ApiImplicitParam(name="name",value="用戶名",dataType="string", paramType = "query",example="xingguo"), @ApiImplicitParam(name="id",value="用戶id",dataType="long", paramType = "query")}) public void select(){ } ``` ? 效果圖:? ## paramType 示例詳解 ### path ```java @RequestMapping(value = "/findById1/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION\_JSON\_UTF8\_VALUE) @PathVariable(name = "id") Long id ``` ### body ``` @ApiImplicitParams({ @ApiImplicitParam(paramType = "body", dataType = "MessageParam", name = "param", value = "信息參數", required = true) }) @RequestMapping(value = "/findById3", method = RequestMethod.POST, produces = MediaType.APPLICATION\_JSON\_UTF8\_VALUE, consumes = MediaType.APPLICATION\_JSON\_VALUE) @RequestBody MessageParam param ``` 提交的參數是這個對象的一個json,然后會自動解析到對應的字段上去,也可以通過流的形式接收當前的請求數據,但是這個和上面的接收方式僅能使用一個(用@RequestBody之后流就會關閉了) ### header ``` @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "Long", name = "id", value = "信息id", required = true) }) String idstr = request.getHeader("id"); if (StringUtils.isNumeric(idstr)) { id = Long.parseLong(idstr); } ``` ### Form ``` @ApiImplicitParams({ @ApiImplicitParam(paramType = "form", dataType = "Long", name = "id", value = "信息id", required = true) }) @RequestMapping(value = "/findById5", method = RequestMethod.POST, produces = MediaType.APPLICATION\_JSON\_UTF8\_VALUE, consumes = MediaType.APPLICATION\_FORM\_URLENCODED\_VALUE) ``` ## 總結: ? - 對于@ApiImplicitParam的paramType:query、form域中的值需要使用@RequestParam獲取, header域中的值需要使用@RequestHeader來獲取,path域中的值需要使用@PathVariable來獲取,body域中的值使用@RequestBody來獲取,否則可能出錯;而且如果paramType是body,name就不能是body,否則有問題,與官方文檔中的“If?paramType?is "body", the name should be "body"不符。 - @ApiImplicitParams:用在方法上包含一組參數說明 - @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數的各個方面 - paramType:參數放在哪個地方 : |類型| 作用 |參數值獲取| |--|--|--| path |以地址的形式提交數據 |@PathVariable query| 直接跟參數完成自動映射賦值 |@RequestParam body |以流的形式提交 僅支持POST | @RequestBody header| 參數在request headers 里邊提交 |@RequestHeader form |以form表單的形式提交 僅支持POST |@RequestParam name:參數名 dataType:參數類型 required:參數是否必須傳 value:參數的意思 defaultValue:參數的默認值 @ApiResponses:用于表示一組響應 @ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應信息 code:數字,例如400 message:信息,例如"請求參數沒填好" response:拋出異常的類 以上這些就是最常用的幾個注解了。 ## 項目例子 ~~~ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; @RestController @RequestMapping("/user") @Api("userController相關api") public class UserController { @Autowired private UserService userService; @ApiOperation("獲取用戶信息") @ApiImplicitParams({ @ApiImplicitParam(paramType="header",name="username",dataType="String",required=true,value="用戶的姓名",defaultValue="zhaojigang"), @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="用戶的密碼",defaultValue="wangna") }) @ApiResponses({ @ApiResponse(code=400,message="請求參數沒填好"), @ApiResponse(code=404,message="請求路徑沒有或頁面跳轉路徑不對") }) @RequestMapping(value="/getUser",method=RequestMethod.GET) public User getUser(@RequestHeader("username") String username, @RequestParam("password") String password) { return userService.getUser(username,password); } } ~~~ 測試: 啟動服務,瀏覽器輸入"http://localhost:8080/swagger-ui.html" ?![](https://img2018.cnblogs.com/blog/1500466/201906/1500466-20190611152142619-2068012481.png) 在上面案例中我們可以知道如果在request域中我們使用reques.getHeader()和使用@RequestHeader注解作用是一樣的,其它內容類似。 ~~~ @ApiResponses:用于表示一組響應 @ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應信息 code:數字,例如400 message:信息,例如”請求參數沒填好” response:拋出異常的類 ~~~ ~~~ @ApiOperation("獲取用戶信息") @ApiImplicitParams({@ApiImplicitParam(paramType="header",name="name",dataType="String",required=true,value="用戶的姓名",defaultValue="zhaojigang"), @ApiImplicitParam(paramType="query",name="pwd",dataType="String",required=true,value="用戶的密碼",defaultValue="wangna") }) @ApiResponses({ @ApiResponse(code=400,message="請求參數沒填好"), @ApiResponse(code=404,message="請求路徑沒有或頁面跳轉路徑不對") }) @RequestMapping(value="/getUser",method= RequestMethod.GET) public User getUser(@RequestHeader("name") String name,@RequestParam("pwd") String pwd) { System.out.println(name); System.out.println(pwd); return userRepository.getUserByNameAndPwd(name,pwd); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看