Record modification of Swagger request body causing problems.

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值