Swagger Documentation for your java project

Swagger documentation using Swagger UI in your java project.

Yes, Swagger UI can be used in a Java project without Spring as well. The process involves manual configuration of the Swagger components and endpoints. Here's how you can set up Swagger UI without Spring:

Step 1: Add Swagger Dependencies Add the following dependencies to your pom.xml file if you are using Maven:

<dependencies>
    <!-- Swagger Dependencies -->
    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-core</artifactId>
        <version>2.1.5</version>
    </dependency>
    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-jaxrs2</artifactId>
        <version>2.1.5</version>
    </dependency>
    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
        <version>2.1.5</version>
    </dependency>
</dependencies>

Step 2: Create Swagger Configuration Create a new Java class (e.g., SwaggerConfig) to configure Swagger manually. This class will be responsible for defining your API documentation. Here's an example of the configuration:

import io.swagger.jaxrs2.DefaultParameterExtension;
import io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder;
import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class SwaggerConfig extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        OpenAPI oas = new OpenAPI().info(new Info()
                .title("Your API Title")
                .description("Your API Description")
                .version("1.0.0"));

        new JaxrsOpenApiContextBuilder<>()
                .servletConfig(config)
                .application(null) // Replace with your JAX-RS Application class if you have one
                .openApiConfiguration(oas)
                .buildContext(true);
    }
}

Step 3: Register SwaggerConfig Servlet In your web.xml file (create one if it doesn't exist), register the SwaggerConfig servlet:

<web-app>
    <servlet>
        <servlet-name>SwaggerConfig</servlet-name>
        <servlet-class>com.your.package.SwaggerConfig</servlet-class> <!-- Replace with the correct package for SwaggerConfig class -->
    </servlet>

    <servlet-mapping>
        <servlet-name>SwaggerConfig</servlet-name>
        <url-pattern>/swagger-ui/*</url-pattern>
    </servlet-mapping>
</web-app>

Step 4: Run the Application Now, you can run your Java application. Swagger UI should be available at http://localhost:8080/swagger-ui/ (assuming your application runs on port 8080). You can access the Swagger UI in your web browser to explore and interact with your API documentation.

With these steps, you have set up Swagger UI in your Java project without relying on the Spring framework. Make sure to keep your OpenAPI Specification up-to-date to maintain accurate documentation.

For Spring projects

To set up Swagger UI in a Java project, you will need to follow these steps:

Step 1: Add Swagger Dependencies In your Java project, you need to add the necessary dependencies for Swagger. You can do this by adding the following Maven dependencies to your pom.xml file:

<dependencies>
    <!-- Swagger Dependencies -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
</dependencies>

Step 2: Enable Swagger in Spring Boot Application If you are using Spring Boot, you need to configure Swagger in your main application class or a configuration class. Create a new Java class (e.g., SwaggerConfig) and add the following configuration:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.your.package.controller")) // Replace with your base package
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Your API Title")
                .description("Your API Description")
                .version("1.0.0")
                .build();
    }
}

Ensure that you replace "com.your.package.controller" with the base package that contains your API controllers.

Step 3: Run the Application Now, you can run your Java application. By default, Swagger UI should be available at http://localhost:8080/swagger-ui.html (assuming your application runs on port 8080). You can access the Swagger UI in your web browser to explore and interact with your API documentation.

That's it! You have successfully set up Swagger UI in your Java project to generate interactive API documentation. Remember to keep your OpenAPI Specification up-to-date as your API evolves to ensure that the documentation remains accurate.