简体   繁体   中英

Can I obtain the Swagger documentation from pre existing Java code?

I am absolutly new in Swagger and I have the following doubt:

I know that gnerally I have to create Swagger document before code my REST API and the use this document to create my API (I know that from the Swagger Editor I can also generate my API server automatically).

My problem is the following one:

I am working on a Java application (a Spring Boot application) that implements my REST API. I want to use Swagger to create my API documentation.

Exist a way to automatically do it? From my Java code to the Swagger yaml file? For example annoting my Java code in some way that could be parsed by some tool?

Yes, there is a tool which can easily generate the swagger documentation from the code you have already written.

This included into a Spring Appllication will create the documentation https://springfox.github.io/springfox/docs/current/

To my mind this is the right way to it. Don't create a documentation and have the code generated, rather generate the documentation. That's the way Javadoc is created as well.

You don't need to annotate nothing in particular.

Create a class similar to the following to access the API with Swagger :

@Configuration
@EnableSwagger2
public class SwaggerConfig{

  @Bean
  public Docket api(){

    return new Docket(DocumentationType.SWAGGER_2).select().apis(
        RequestHandlerSelectors.basePackage("com.yourcompany.restcode")).paths(PathSelectors.any()).build();
  }
}

Then access the API documentation with something like:

localhost:8080/swagger-ui.html#/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM