Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ ApiImplicitParams, ApiImplicitParam

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use @ ApiImplicitParams, ApiImplicitParam". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use @ ApiImplicitParams, ApiImplicitParam".

Catalogue

@ ApiImplicitParam

Parameters.

Type function

@ ApiImplicitParams

Detailed explanation of paramType example

Make a brief summary

test

@ ApiImplicitParam

Acting on the method, representing a separate request parameter

Parameters.

Name: parameter name.

Value: the specific meaning and function of the parameters.

Required: whether the parameter is required.

DataType: the data type of the parameter.

ParamType: query parameter type. There are several forms here:

Type function

Path submits data in the form of address

Query completes automatic mapping assignment directly with parameters.

Only POST is supported when body is submitted as a stream

The header parameter is submitted in request headers

Form submission in the form of form forms supports only POST

I was tricked once here: when I sent a POST request, the whole parameter accepted at that time, whether I used body or query, the background would report a Body Missing error.

This parameter conflicts with @ RequestBody in SpringMvc, so I simply removed paramType, which has no effect on interface testing.

@ ApiImplicitParams

For methods, including multiple @ ApiImplicitParam:

Example:

@ ApiOperation ("query Test") @ GetMapping ("select") / / @ ApiImplicitParam (name= "name", value= "user name", dataType= "String", paramType = "query") @ ApiImplicitParams ({@ ApiImplicitParam (name= "name", value= "user name", dataType= "string", paramType = "query", example= "xingguo"), @ ApiImplicitParam (name= "id", value= "user id", dataType= "long", paramType = "query") public void select () {} paramType example

Path

@ 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 = "information parameter", required = true)}) @ RequestMapping (value = "/ findById3", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @ RequestBody MessageParam param

The submitted parameter is a json of the object, and then it is automatically parsed to the corresponding field, or the current request data can be received in the form of a stream, but only one of this and the above receiving method can be used (the stream will be closed after using @ RequestBody)

Header

@ ApiImplicitParams ({@ ApiImplicitParam (paramType = "header", dataType = "Long", name = "id", value = "information 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 = "Information id", required = true)}) @ RequestMapping (value = "/ findById5", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)

(1) the values in the paramType:query and form fields of @ ApiImplicitParam need to be obtained using @ RequestParam, the values in the header domain need to be obtained using @ RequestHeader, the values in the path domain need to be obtained using @ PathVariable, and the values in the Body field need to be obtained using @ RequestBody, otherwise errors may occur. If paramType is body,name, it cannot be body, otherwise there is a problem, which does not match "If paramType is" body "and the name should be" body "in the official document.

@ ApiImplicitParams: used to include a set of parameter descriptions on a method

ApiImplicitParam: used in the @ ApiImplicitParams annotation to specify all aspects of a request parameter

ParamType: where to put the parameter

Acquisition of header-- > request parameters: @ RequestHeader

Acquisition of query-- > request parameters: @ RequestParam

Path (for restful API)-- > acquisition of request parameters: @ PathVariable

Body (not commonly used)

Form (not commonly used)

Name: parameter name

DataType: parameter typ

Required: whether the parameter must be passed

Value: the meaning of the parameter

DefaultValue: default value of the parameter

@ ApiResponses: used to represent a set of responses

@ ApiResponse: used in @ ApiResponses to express an error response message

Code: a number, such as 400

Message: information, such as "request parameters are not filled"

Response: class that throws an exception

These are the most commonly used notes.

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 related api") public class UserController {@ Autowiredprivate UserService userService @ ApiOperation ("get user information") @ ApiImplicitParams ({@ ApiImplicitParam (paramType= "header", name= "username", dataType= "String", required=true,value= "user's name", defaultValue= "zhaojigang"), @ ApiImplicitParam (paramType= "query", name= "password", dataType= "String", required=true,value= "user's password", defaultValue= "wangna")}) @ ApiResponses ({@ ApiResponse (code=400,message= "request parameters are not entered"), @ ApiResponse (code=404) Message= "request path is not available or page jump path is incorrect") @ RequestMapping (value= "/ getUser", method=RequestMethod.GET) public User getUser (@ RequestHeader ("username") String username, @ RequestParam ("password") String password) {return userService.getUser (username,password) }} Test

Start the service and enter "http://localhost:8080/swagger-ui.html"" in the browser

In the above case, we can see that if we use reques.getHeader () in the request field, the effect is the same as using the @ RequestHeader annotation, and the rest is similar.

@ ApiResponses: used to represent a set of responses

@ ApiResponse: used in @ ApiResponses to express an error response message

Code: a number, such as 400

Message: information, such as "request parameters are not filled"

Response: class that throws an exception

@ ApiOperation ("get user information") @ ApiImplicitParams ({@ ApiImplicitParam (paramType= "header", name= "name", dataType= "String", required=true,value= "user's name", defaultValue= "zhaojigang"), @ ApiImplicitParam (paramType= "query", name= "pwd", dataType= "String", required=true,value= "user's password", defaultValue= "wangna")}) @ ApiResponses ({@ ApiResponse (code=400,message= "request parameters are not entered"), @ ApiResponse (code=404) Message= "No request path or incorrect page jump path") @ 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);} Thank you for reading, the above is the content of "how to use @ ApiImplicitParams, ApiImplicitParam". After the study of this article, I believe you have a deeper understanding of how to use @ ApiImplicitParams and ApiImplicitParam, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report