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

Question: what's the preferred way to override the v2 path? #645

Closed
who opened this issue Apr 2, 2015 · 29 comments

Comments

@who
Copy link
Contributor

commented Apr 2, 2015

The default base path for Swagger 2.0 spec is /v2/api-docs.

I would like to change this to be just /api-docs.

Unless I have the syntax wrong, from the looks of the @RequestMapping annotation in Swagger2Controller, there's a config parameter that can be used to do this. However, even after adding springfox.documentatation.swagger.v2.path=/api-docs to my config, the path doesn't change.

I am loading my config strings via a @Bean public static PropertySourcesPlaceholderConfigurer loadConfig() function. My other config strings load and are available after the PropertySourcesPlaceholderConfigurer does it's duty.

@dilipkrish dilipkrish added the question label Apr 2, 2015

@dilipkrish

This comment has been minimized.

Copy link
Member

commented Apr 2, 2015

s/documentatation/documentation/g?

@who

This comment has been minimized.

Copy link
Contributor Author

commented Apr 2, 2015

No, that's the actual config string referenced in the controller.

From Swagger2Controller.java on master:

...
@RequestMapping(value = "${springfox.documentatation.swagger.v2.path:/v2/api-docs}", method = RequestMethod.GET)
...
@Aloren

This comment has been minimized.

Copy link

commented Apr 2, 2015

@who I have the same issue. See: #642 :)

@dilipkrish dilipkrish closed this in e8139a5 Apr 2, 2015

@dilipkrish

This comment has been minimized.

Copy link
Member

commented Apr 2, 2015

Typo fixed but @Aloren is your property override working for you?

@dilipkrish dilipkrish added the duplicate label Apr 2, 2015

@Aloren

This comment has been minimized.

Copy link

commented Apr 2, 2015

@dilipkrish, yes. That works fine. @who, if you need help resolving the issue, feel free to ask :)

@who

This comment has been minimized.

Copy link
Contributor Author

commented Apr 2, 2015

@Aloren Yes, I am still having an issue with this. My config setting of springfox.documentation.swagger.v2.path=/api-docs isn't being populated into the @RequestMapping of SwaggerController2, so the Swagger2 spec is still being served from /v2/api-docs instead of /api-docs.

@dilipkrish

This comment has been minimized.

Copy link
Member

commented Apr 2, 2015

@who is your PropertySourcesPropertyConfigurer a statically defined bean?

@Aloren

This comment has been minimized.

Copy link

commented Apr 2, 2015

@who add something like that to your code:

@PropertySource("classpath:swagger.properties")
@Configuration
public class SwaggerConfiguration {

//By this properties you can check which values were actually injected (should be used in tests)
@Value("${springfox.documentation.swagger.v2.path}")
private String swagger2Endpoint;
@Value("${springfox.documentation.swagger.v1.path}")
private String swagger1Endpoint;

}

If you follow standard maven folder layout -- add swagger.properties with the following content to src/main/resources folder:
springfox.documentation.swagger.v2.path=/api-docs
springfox.documentation.swagger.v1.path=/v1/api-docs

Make sure that SwaggerConfiguration's package is included into @componentscan basePackages value.

@who

This comment has been minimized.

Copy link
Contributor Author

commented Apr 2, 2015

@who is your PropertySourcesPropertyConfigurer a statically defined bean?

@dilipkrish Yes, it is.

@Aloren I am using new ClassPathResource("myconfig.properties") to load the file instead of @PropertySource. I'll give the latter a shot.

@Aloren

This comment has been minimized.

Copy link

commented Apr 2, 2015

@who I haven't used ClassPathResource, but I think that is not a place from where properties get resolved by Spring. Have a look at: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.html

@crm86

This comment has been minimized.

Copy link

commented Apr 27, 2015

I have the same issue. What is the final solution? I am still getting the default value for the version 2

@dilipkrish

This comment has been minimized.

Copy link
Member

commented Apr 27, 2015

The solution is to use @PropertySource

@gambour

This comment has been minimized.

Copy link

commented Apr 20, 2016

@configuration
@EnableSwagger2
@PropertySource({"classpath:swagger.properties"})
@ConditionalOnResource(resources = {"classpath:swagger.properties"})
@Profile("!test")
public class SwaggerConfig extends WebMvcAutoConfigurationAdapter {

and then in swagger.properties you put:

springfox.documentation.swagger.v2.path=rest/v1/api-docs

it works for me 👍

@prathap413

This comment has been minimized.

Copy link

commented Jul 21, 2016

I have a issue changing the Swagger-Ui URL. In integrated swager2 and swagger-ui using below dependencies

io.springfox
springfox-swagger2
2.4.0

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.4.0</version>
    </dependency>

Right now i am able to access to swagger Ui using : http://localhost:8080//rest/swagger-ui.html

So i want to change that URL to : http://localhost:8080/swagger or http://localhost:8080//swagger.

Can any one help me with this.

@miguelschwindt

This comment has been minimized.

Copy link

commented Aug 3, 2016

I'm using Spring 4.3 and Swagger v2.

The solution of @gambour works fine for me.

@gambour

This comment has been minimized.

Copy link

commented Aug 3, 2016

@prathap413 this topic is talking about customising v2 path and not swagger-ui.html
swagger-ui.html is located in a webjar (added by spring boot to resources, see WebMvcAutoConfiguration addResourceHandlers)

to override this i think you should copy this file into your web resource or configure spring mvc so that the path /swagger show you your swagger-ui.html.

@umbreak

This comment has been minimized.

Copy link

commented Nov 17, 2016

Spring Boot 1.4.1
Springfox 2.6.0
The following configuration does not work for me:

@SpringBootApplication
@EnableSwagger2
@PropertySource("classpath:application.properties")
public class Application {
    //Oher configuration here...
    @Value("${springfox.documentation.swagger.v2.path}")
    private String swagger2Endpoint;

    @Bean
    public Docket swaggerSettings() {
        List<SecurityScheme> scheme = Lists.newArrayList(new BasicAuth("basicAuth"));
        return new Docket(DocumentationType.SWAGGER_2)
                .alternateTypeRules(newRule(typeResolver.resolve(DeferredResult.class,typeResolver.resolve(FileSystemResource.class)),
                                typeResolver.resolve(MultipartFile.class)))
                .securitySchemes(scheme)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .protocols(Sets.newHashSet("http"));
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

And the application.properties (inside resources folder) contains: springfox.documentation.swagger.v2.path=/api-docs

However, on start, in the logs I still see:

[main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)`
@dilipkrish

This comment has been minimized.

Copy link
Member

commented Nov 21, 2016

@umbreak this is fixed in 2.6.1

@kunal-bhatia

This comment has been minimized.

Copy link

commented Nov 21, 2016

I'm facing the same issue, even upgrading the version to 2.6.1
resources/swagger.properties springfox.documentation.swagger.v2.path=/rest/docs
swaggerconfig:-

@PropertySource("classpath:swagger.properties")
@Value("${springfox.documentation.swagger.v2.path}")
private String swagger2Endpoint;

In the logs, I still see the same:-

`s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)`
@dilipkrish

This comment has been minimized.

Copy link
Member

commented Nov 21, 2016

You'd need to put your property source annotation @ the class level typically a @Configuration bean

@kunal-bhatia

This comment has been minimized.

Copy link

commented Nov 22, 2016

@dilipkrish ,
I have put the @PropertySource annotation on the class level, just like @configuration. But still it does not work for me. I'm able to see the changed values in local, but when running on other environments, its still /v2/api-docs.

@umbreak

This comment has been minimized.

Copy link

commented Nov 22, 2016

I can confirm that in 2.6.1 it works

@kunal-bhatia

This comment has been minimized.

Copy link

commented Dec 2, 2016

I'm using an old version of spring boot i.e.
Running with Spring Boot v1.2.7.RELEASE, Spring v4.1.8.RELEASE. It does not work for me in the same. could that be a reason

@bobwenx

This comment has been minimized.

Copy link

commented May 5, 2017

2.6.1 does not work for me in the same.

@dilipkrish

This comment has been minimized.

Copy link
Member

commented May 5, 2017

@bobwenx There are some improvements in the upcoming 2.7.0 release that will make this better.

@shivako

This comment has been minimized.

Copy link

commented Jun 2, 2017

Any help is appreciated. If I need to implement my own security ( I need to get access-token from Response's headers etc and stick this token with all further requests). How to do this kind of customization?

@kosikfl1

This comment has been minimized.

Copy link

commented Sep 8, 2017

I only have my phone. My computer is broken. I'm trying to get money to get one and then come back.

@dilipkrish

This comment has been minimized.

Copy link
Member

commented Sep 14, 2017

@xbmono

This comment has been minimized.

Copy link

commented Aug 9, 2019

This works for me and I can see springfox request is being sent to /my/docs the path I configured and I have an endpoint that is request mapped to that path but it still returns the default docs and my endpoint is not being called. Have I made a mistake or I didn't understand the whole point of this setting? Note: I want to return my own swagger.json instead of the one that springfox is using/generating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.