Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan ยท April More text Challenge”. Click here for more details.
1. Preface ๐ฅ
๐จ๐ Author: Bug Bacteria
โ๏ธ blog: CSDN, Nuggets, etc
๐ public account: Magic House of the Circle of the Apes
๐ซ special statement: original is not easy, reprint please attach the original source link and this article statement, thank you for your cooperation.
๐ Copyright notice: part of the text or pictures in the article may come from the Internet or Baidu Encyclopedia, if there is infringement, please contact bug bacteria processing.
Hello, little friends, I am bug bacteria ๐, unconsciously on a year and April, is a good season for outing. But the epidemic in Shanghai, in addition to telecommuting at home is to watch technology incidentally volume some articles. This is not another article activity in April, push yourself, do some content output every day, develop the habit of writing, in the future you will come back to thank yourself for your perseverance to have the brilliant achievements at this moment.
In the process of reviewing articles, if you think the articles are helpful to you at all, please don’t be too mean with your likes and bravely light up the articles ๐. Your likes (collect โญ๏ธ+ pay attention to ๐จ port + message board) are the best encouragement and support for bugs on my creation path. Time does not abandon ๐๐ปโ๏ธ, nuggets stop ๐, cheer up ๐ป
Ii. Background ๐ฅ
Speaking of Swagger, many friends are certainly familiar with Swagger, a super easy to use postman integrated in the project, but for general interface testing, it is very simple.
The general rule is to use the body parameter body and nothing else. However, for some of the “often” operation, it is not so familiar with use, but a basic understanding of it provided by the API, you can also change its source, but I want to say today is neither are you familiar with the realization of its source, also need not to take extra studies other technologies, only need to see I write, I can teach understand your today’s theme, “How to attach custom headers to swagger requests?” .
For example, if your login is authenticated by token, one of the things you will do is require the front-end to have a token attached to the request header in every HTTP request, right? This is also a common login interception function in business systems. I’m going to take this example today, and it’s very simple how to write it.
3. Solution ๐ฅ
If you don’t have swagger in your project, you won’t be able to use swaggerConfig. Java.
What’s the connection with swagger adding custom headers? Don’t worry, keep reading.
We all know that the body argument comes with it, so what if you want to add a header argument? That is about to manually add the matter with you, you only need to inject the globalOperationParameters (), it is a global parameters configuration, you only need to set your name set in the header parameter.
Let’s look at globalOperationParameters where () method to add.
/ swagger configuration document * / * * * @ Bean public Docket customDocket () {return new Docket (DocumentationType. SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.review.controller")) .paths(PathSelectors.any()) .build() .globalOperationParameters(); // global configuration}Copy the code
You can then either pass in the header arguments you want to set in the current class, but I’ve wrapped a separate built-in method for adding arguments, which has the advantage of not making the code too long.
/** * Add the head Parameter */ private List<Parameter> getParameterList() {ParameterBuilder clientIdTicket = new ParameterBuilder(); List<Parameter> pars = new ArrayList<Parameter>(); Clientidticket.name (constant.token_herder_key).description("token ").modelref (new modelRef("string")) .parameterType("header") .required(false).build(); // If set to false, the clientId parameter is optional. pars.add(clientIdTicket.build()); return pars; }Copy the code
If you want to add multiple header parameters, create a new ParameterBuilder() class and add your build parameter to the pars collection. Constant is a Constant class that I defined, if you’re not used to naming strings directly.
The final step is to pass the currently encapsulated header into the global configuration method.
.globalOperationParameters(this.getParameterList());
Copy the code
Can you see that? If not, I will paste all swagger configuration below, everyone will read it in a series of ways, basically read what I said, next time if you meet with header add custom request header, directly hate it, this I will be born.
Below is the complete Swagger configuration.
package com.example.review.config; import com.example.review.util.Constant; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.schema.ModelRef; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.service.Parameter; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; /** * Swagger Configuration ** @author * @date 2021-06-01 13:00 */ @configuration / @enablesWagger2 must exist // Public class must exist SwaggerConfig {/ swagger configuration document * / * * * @ Bean public Docket customDocket () {return new Docket (DocumentationType. SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.review.controller")) .paths(PathSelectors.any()) .build() .globalOperationParameters(this.getParameterList()); // Global configuration} /** * Add head parameters */ private List<Parameter> getParameterList() {ParameterBuilder clientIdTicket = new ParameterBuilder(); List<Parameter> pars = new ArrayList<Parameter>(); Clientidticket.name (constant.token_herder_key).description("token ").modelref (new modelRef("string")) .parameterType("header") .required(false).build(); // If set to false, the clientId parameter is optional. pars.add(clientIdTicket.build()); return pars; } /** * API related configuration */ private ApiInfo ApiInfo () {return new ApiInfoBuilder().title(" Swagger API documentation ").contact(new) Contact("review", "", "").description(" swagger-bootstrap-UI ").termsofServiceurl ("http://localhost:8888/").version("1.0").build(); }}Copy the code
4. Test ๐ฅ
After configuration, restart the project to refresh the Swagger access link. No, the header type is not added. Isn’t it easy to configure directly so that everyone doesn’t have to go to Postman for interface testing?
. .
Ok, that’s it. If you want to learn more, you can read my previous recommendations. Accumulate a little weird knowledge every day, and over a long period of time, you will definitely become a respected leader. Well, I’ll see you next time
Five, the previous recommendation ๐ฅ
- How to use Mybatis- Plus to automatically fill field contents? If not, I’ll teach you.
- Springboot series (16) : Integrated easyPOI to realize word template circulation export multiple data
- Springboot series (16) : Integrated easyPOI word template image export
- Springboot series (16) : integrated easyPOI to achieve excel multi-sheet import
- Springboot series (16) : integrated easyPOI to achieve excel multi-sheet export
Six, the end of the article ๐ฅ
If you want to learn more, you can pay attention to the bug bug column “SpringBoot Zero-based Introduction”, from scratch, from zero to one! Hope I can help you.
I am bug fungus, a want to go ๐ฃ out of the mountain to change the fate of the program ape. The next road is still very long, waiting for us to break through, to challenge. Come on, friends, let’s come on! Fighting for the future!
Finally, I like to send you two words, and you share!
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
โ๏ธ Be who you want to be, there is no time limit, you can start whenever you want,
๐ You can change from now on, you can also stay the same, this thing, there are no rules to speak of, you can live the most wonderful yourself.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
โ
๐ If this article is helpful to you, please leave a like! (# ^. ^ #);
๐ if you like the article shared by bug fungus, please give bug fungus a point of concern! The danjun ‘แด, you guys will have a cameo appearance with you.
๐ if you have any questions about the article, please also leave a message at the end of the article or add a group [QQ communication group :708072830];
๐ In view of the limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (do not post offensive comments, thank you);
๐ copyright notice: original is not easy, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate!! thank you