This article will record the resolution process of the following problems
目录
1. When Swagger request body is JSONObject, how to display detailed parameters in the API document?
2. What the difference between Swagger2 and Swagger3 about the JWT authenticate?
3. Resolve maven does not find JUnit tests to run.
1. When Swagger request body is JSONObject, how to display detailed parameters in the API document?
Due to project reasons, the request of some of our APIs cannot be set to a custom object and can only be set to JSONObject. So this time the task is to solve the problem of displaying detailed parameters in swagger when the request body is set to JSONObject. Our swagger version is 2.8.0 and springboot is 2.1.2. For resolve the task I upgrade swagger to 3.0.0. and springboot to 2.3.2.
Here is a code snippet of how it can be done.
Code in controller:
@PostMapping(value = "/example")
@ApiImplicitParam(name = "requestBody", dataTypeClass = CustomType.class)
public Result put2Connections(@RequestBody JSONObject requestBody) {
...
}
Code in swagger config:
public Docket createRestApi(TypeResolver resolver) {
return new Docket(DocumentationType.OAS_30)
// enable JWT validation
.securityContexts(Collections.singletonList(securityContext()))
.securitySchemes(Collections.singletonList(apiKey()))
.apiInfo(apiInfo())
// Support describing JSONObject input [*important*]
.additionalModels(resolver.resolve(CustomType.class))
//Whether to turn on (true, false to hide. Production environment is suggested to be hidden)
.enable(enableSwagger)
.select()
//Scan the path package. Setting basepackage will take all methods of @ API marked classes under the package as APIs
.apis(RequestHandlerSelectors.basePackage("xxx"))
//Specified path processing PathSelectors.any () represents all paths
.paths(PathSelectors.any())
.build();
}
Reference link is: https://blog.csdn.net/hellopeng1/article/details/82227942?fireglass_rsn=true
2. What the difference between Swagger2 and Swagger3 about the JWT authenticate?
Authenticate code:
private ApiKey apiKey() {
return new ApiKey(AUTHORIZATION, AUTHORIZATION, "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth()).build();
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Collections.singletonList(new SecurityReference(AUTHORIZATION, authorizationScopes));
}
The difference between 2 and 3 key method is ApiKey(), in swagger2 header name is keyname and for 3 is used name.
public ApiKey(String name, String keyname, String passAs) {
this(name, keyname, passAs, new ArrayList());
}
3. Resolve maven does not find JUnit tests to run.
Use "mvn clean test" run Junit4 need the junit-vintage-engine.
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
Reference link is: https://stackoverflow.com/questions/6178583/maven-does-not-find-junit-tests-to-run