0

I have generated a JHipster application without a frontend, thus I wanted to congfigure swagger ui on the server side.

I ended up having to define a Docket and WebMvcEndpointHandlerMapping bean which seem to do the trick.

In the swagger UI, in order to have the bearer token (added in the ‘Authorize’ dialog) automatically appended as a request header to api calls I had to however add an annotation to the method in my rest controller

@ApiOperation(value = “…” , authorizations = {@Authorization(value = "Bearer")})
@GetMapping("/account")
public AdminUserDTO getAccount() {
...

I just wondered if there was any easy way to apply this at a class or package level in one fell swoop ?

Here is my SpringFox config fwiw

@Configuration
public class SpringFoxConfig {

    @Value("${spring.application.name}")
    private String applicationName;


    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
            .paths(PathSelectors.any())
            .build()
            .securitySchemes(Arrays.asList(
                new ApiKey("Bearer", HttpHeaders.AUTHORIZATION, In.HEADER.name())))
            .apiInfo(new ApiInfo(applicationName, "TODO", "1.0", "",
                new Contact("..., "", null), "", "", Arrays.asList(new VendorExtension() {
                @Override
                public String getName() {
                    return "";
                }

                @Override
                public Object getValue() {
                    return "null";
                }
            })));
    }

    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }

    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }
}

Also had to set

spring.mvc.pathmatch.matching-strategy=ant-path-matcher

1977
  • 2,580
  • 6
  • 26
  • 37

0 Answers0