Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate swagger JSON file at compile time for springfox based project

I have working project which is using springfox to generate API documentations.

I want to generate swagger.json at compile time.

following is sample springfox configuration,

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()                 
                .apis(RequestHandlerSelectors.basePackage("com.abc.xyz"))
                .paths(regex("/*.*"))
                .build();
    }
}

FYI : I have also tried https://github.com/kongchen/swagger-maven-plugin plugin but it's not working

like image 705
Mayur Avatar asked Dec 19 '18 10:12

Mayur


People also ask

What is Springfox swagger?

Springfox is a framework that acts as the “glue” between Swagger and Spring. It generates the specification (contract) based on your code and also deploys the Swagger UI client with your application, allowing you to immediately test your REST API.

Where can I find swagger JSON file?

Launch the app, and navigate to http://localhost:<port>/swagger/v1/swagger.json . The generated document describing the endpoints appears as shown in Swagger specification (swagger. json). The Swagger UI can be found at http://localhost:<port>/swagger .

What is Springfox’s swagger?

The good news is that SpringFox can generate Swagger documentation based on such annotations, so you can utilize what you already have in your project without writing all the constraints manually! It is very useful as consumers of your API know what are restrictions on the values they should provide to your API and what values to expect.

How to generate the API interface and models using Swagger?

Gradle Normally when using swagger, you generate a swagger.yaml file for your API. But what if you already have a swagger.yaml file and you want to generate the API interface and models, like you would also do with a webservice using a WSDL file? To achieve this, swagger has a great tool: swagger-codegen.

How to convert JSON to HTML in Swagger online?

Just go to the Swagger Online Editor and paste the JSON there. Paste your generated JSON to the left panel and voila! You can now see your generated documentation as HTML page. Nice, isn't it? It would be even nicer to have such documentation directly as a part of your application. Fortunately, it is quite easy to achieve this.

What is inputspec in Swagger Codegen?

The inputSpec is our swagger file describing our API. This can also be a json file, or a link to a hosted swagger file. The output directory is the same as our project directory. This is because swagger-codegen generates a whole project including a pom.xml.


1 Answers

This is achieved using JUnit test case, follow https://github.com/springfox/springfox/issues/1959 for more details.

like image 55
Mayur Avatar answered Oct 23 '22 18:10

Mayur