Spring Boot Project with Swagger
1. Setting Up Your Spring Boot Project
Step 1: Use Spring Initializr
Go to Spring Initializr.
Select your project metadata:
- Project: Maven Project
- Language: Java
- Spring Boot: (Select the latest stable version)
- Group:
com.example - Artifact:
swagger-demo - Dependencies: Add
Spring WebandSpringfox Swagger UI.
Click on Generate to download the project ZIP file. Extract it to your preferred location.
Step 2: Import the Project into Your IDE
- Open your favorite IDE (e.g., IntelliJ IDEA, Eclipse).
- Import the project as a Maven project.
2. Creating a Simple REST API
Step 1: Create a Model Class
Create a new package com.example.swaggerdemoproject.model and create a class named User.
package com.example.swaggerdemoproject.model;
public class User {
private Long id;
private String name;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 2: Create a Controller Class
Create a new package com.example.swaggerdemoproject.controller and create a class named UserController.
package com.example.swaggerdemoproject.controller;
import com.example.swaggerdemoproject.model.User;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/api/users")
public class UserController {
private List<User> users = new ArrayList<>();
@GetMapping
public List<User> getAllUsers() {
return users;
}
@PostMapping
public User createUser(@RequestBody User user) {
users.add(user);
return user;
}
}
3. Adding Swagger Dependencies
Step 1: Update pom.xml
Ensure that you have the following Swagger dependencies in your pom.xml:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Step 2: Enable Swagger Configuration
Create a configuration class SwaggerConfig in the package com.example.swaggerdemoproject.config.
package com.example.swaggerdemoproject.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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.example.swaggerdemoproject.controller"))
.paths(PathSelectors.any())
.build();
}
}
4. Running the Application
Step 1: Run Your Application
- In your IDE, run the
SwaggerDemoApplicationclass (usually located in the root package). - Your application should start successfully on
http://localhost:8080.
5. Viewing the Swagger UI
Step 1: Access Swagger UI
- Open your web browser and go to
http://localhost:8080/swagger-ui/. - You should see the Swagger UI displaying your API endpoints.
Comments
Post a Comment