question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to set up the base path in the Docket configuration (Springfox 3.0.0-SNAPSHOT)

See original GitHub issue
  • Version 3.0.0-SNAPSHOT
  • Feature Request

I am using Springfox 3.0.0-SNAPSHOT in a Spring Boot 2.1.1 WebMvc project. It generates a documentation with basePath : “/” and paths like: /myapp/api/v1/customers. What I would like is basePath: “/myapp” and paths /api/v1/customers.

I have searched for information about how to customize the base path and the endpoints, found this issue where jonaskoperdraat provides a solution and tried it:

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
        		.host("localhost:8081")
                .pathProvider(new PathProvider() {
                	
                   private final String ROOT = "/myapp";
                	
                    @Override
                    public String getOperationPath(String operationPath) {
                        return operationPath.replace(ROOT, "");
                    }
                    @Override
                    public String getResourceListingPath(String groupName, String apiDeclaration) {
                        return null;
                    }
                })

My new paths are ok: /api/v1/customers, but the base path keeps being “/”. I noticed the suggestion of explicitly setting Docket().host() but it didn’t work either.

I have debugged the source code and found this on startup springfox.documentation.spring.web.scanners.ApiDocumentationScanner:

public Documentation scan(DocumentationContext context) {
    ApiListingReferenceScanResult result = apiListingReferenceScanner.scan(context);
    ApiListingScanningContext listingContext = new ApiListingScanningContext(context,
        result.getResourceGroupRequestMappings());

    Map<String, List<ApiListing>> apiListings = apiListingScanner.scan(listingContext);
    Set<Tag> tags = toTags(apiListings);
    tags.addAll(context.getTags());
    DocumentationBuilder group = new DocumentationBuilder()
        .name(context.getGroupName())
        .apiListingsByResourceGroupName(apiListings)
        .produces(context.getProduces())
        .consumes(context.getConsumes())
        .host(context.getHost())
        .schemes(context.getProtocols())
        .basePath(ROOT)
        .extensions(context.getVendorExtentions())
        .tags(tags);

As you can see there’s an explicit .base(ROOT) invocation. Couldn’t DocumentationContext offer a context.getBasePath() method that was set based on another Docket().basePath() operation?

If not, what’s the way to customize this?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
Blokkocommented, Aug 2, 2019

My simple workaround, but I really hope 3.0.0 will released soon 😉

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import io.swagger.models.Swagger;
import springfox.documentation.spring.web.json.JacksonModuleRegistrar;
import springfox.documentation.spring.web.json.Json;
import springfox.documentation.spring.web.json.JsonSerializer;
import springfox.documentation.spring.web.paths.Paths;

@Component
@Primary
public class CustomBasePathJsonSerializer extends JsonSerializer {

    @Value("${server.servlet.context-path}")
    private String contextPath;

    public CustomBasePathJsonSerializer(List<JacksonModuleRegistrar> modules) {
        super(modules);
    }

    @Override
    public Json toJson(Object toSerialize) {
        if (toSerialize instanceof Swagger) {
            Swagger swagger = (Swagger) toSerialize;
            swagger.basePath(contextPath + Paths.ROOT);
        }
        return super.toJson(toSerialize);
    }

}
8reactions
thomasmeycommented, Aug 13, 2020

can someone please reopen this issue?

I keeps me from updating to v3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Springfox Reference Documentation - GitHub Pages
6, paths() allows selection of Path 's using a predicate. ... There are plenty of more options to configure the Docket .
Read more >
Springfox 3.0.0 is not working with Spring Boot 2.6.0 [duplicate]
Up to now, Springfox 3.0.0 only works with Spring 2.6.0-M2 but not with versions above. See the open Springfox issue ...
Read more >
Setting Up Swagger 2 with a Spring REST API - Baeldung
In this tutorial, we'll look at Swagger 2 for a Spring REST web service, using the Springfox implementation of the Swagger 2 specification....
Read more >
How to set up Spring Boot app documentation with Springfox
Add Springfox Boot starter · package in.keepgrowing.scrumally. · import org.springframework.context. · import org.springframework.context. · import ...
Read more >
OpenAPI 3 Library for spring-boot
For custom path of the swagger documentation in HTML format, add a custom springdoc ... For example, if you have the following settings:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found