1
votes

I have followed below link to create API docs for my REST services using Swagger with Spring.

http://jakubstas.com/spring-jersey-swagger-configuration/#comment-1726

Everything goes smooth but when I try to access api docs for swagger using the url http://localhost:8080/rest/api-docs, I am getting Can't read swagger JSON. Can someone please help?

4
Your /rest/api-docs path should contain a swagger spec file. when your browser point to the url and if you installed swagger-ui, it will read from this file and render it to a nice API doc webpage. - Xianlin

4 Answers

0
votes

Swagger don't work in local! You can download Swagger Ui for local

0
votes

You try this way.

@Configuration
@EnableSwagger
// Loads the spring beans required by the framework
public class MySwaggerConfig
{

    private SpringSwaggerConfig springSwaggerConfig;

    /**
     * Required to autowire SpringSwaggerConfig
     */
    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
    {
        this.springSwaggerConfig = springSwaggerConfig;
    }

    /**
     * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
     * framework - allowing for multiple swagger groups i.e. same code base
     * multiple swagger resource listings.
     */
    @Bean
    public SwaggerSpringMvcPlugin customImplementation()
    {
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(
                ".*?");
    }

    private ApiInfo apiInfo()
    {
        ApiInfo apiInfo = new ApiInfo(
                "xx", 
                "xxxx",
                "My Apps API terms of service", 
                "xxx",
                null,
                null);
        return apiInfo;
    }
}







 <dependency>
                <groupId>com.mangofactory</groupId>
                <artifactId>swagger-springmvc</artifactId>
                <version>0.9.5</version>
            </dependency>
0
votes

I solved this issue by adding next file to resource folder

swagger.properties

added there property:

springfox.documentation.swagger.v2.path=/api/swagger.json

and then in code added:

@Configuration
@EnableSwagger2
@PropertySource(value = "classpath:swagger.properties")
public class PathConfiguration {
@Value("${springfox.documentation.swagger.v2.path}")
private String swagger2Endpoint;

and then bean simple bean for Dockect configuration like:

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build();
}
0
votes

it will make this issue as below:

@Bean
public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .groupName("Swagger Group One API")
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.xingyun"))
            .paths(PathSelectors.any())
            .build();
}

fixed as below is okay

@Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("SwaggerGroupOneAPI")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xingyun"))
                .paths(PathSelectors.any())
                .build();
    }