Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide way to set or determine basePath at runtime #219

Open
scoldwell opened this issue Dec 28, 2016 · 1 comment
Open

Provide way to set or determine basePath at runtime #219

scoldwell opened this issue Dec 28, 2016 · 1 comment

Comments

@scoldwell
Copy link

We do not always know the URL of our deployed applications at build time and as such having the basePath hardcoded as part of the build process doesn't work. Even using maven properties to change the path doesn't help with the application being portable.

@osigge
Copy link
Contributor

osigge commented Dec 30, 2016

We had the same problem. We're using Spring Boot with embedded Tomcat and got a workaround for this by using an Interceptor, which overrides the jsondoc configuration at runtime, with the hostname given by the request. The interceptor only listens on the jsondoc path. Here's the code (without the imports):

@Component
public class DocumentationInterceptor extends HandlerInterceptorAdapter {

    @Inject
    private JSONDocProperties jsonDocProperties;

    @Inject
    private JSONDocController jsonDocController;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        final URL baseURLFromRequest = RequestHelper.getBaseURLFromRequest(request);
        jsonDocProperties.setBasePath(baseURLFromRequest.toExternalForm());
        final Field basePath = jsonDocController.getClass().getDeclaredField("basePath");
        basePath.setAccessible(true);
        basePath.set(jsonDocController, jsonDocProperties.getBasePath());
        return super.preHandle(request, response, handler);
    }
}

And for the config (without imports):

@Configuration
public class DocumentationConfig {

    @Bean
    public MappedInterceptor mappedInterceptor(DocumentationInterceptor documentationInterceptor) {
        return new MappedInterceptor(new String[]{"/jsondoc*"}, documentationInterceptor);
    }

}

Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants