
Quick Contact
Java Tutorial
- What is Java?
- History of Java
- Hello Java Program
- Features of Java
- Basic Syntax
- Java Setup
- Data Types in Java
- Java Variables
- Operators in Java
- JVM
- Java If-else Statement
- Switch Case Statement
- Java Loops
- Do-While Loop
- Java While Loop
- Continue Statement
- Break Statement in Java
- Constructors in Java
- Oops Concept in Java
- Features of OOPs
- Inheritance
- Exeception handeling
- Aggregation (HAS-A relationship) in Java
- Method Overriding in Java
- Method Overloading
- Java Static Keyword
- Java This Keyword
- Java Final Keyword
- Polymorphism
- Static Binding and Dynamic Binding
- Abstract class in Java
- Access Modifiers in Java
- Difference between abstract class and interface
- Interface in Java
- Garbage Collection in Java
- Java Package
- Encapsulation
- Serialization and Deserialization in Java
- Java Inner Classes
- Java Applets
- Multithreading in Java
- Thread Priorities in Java
- Thread Creation
- Inter Thread Communication
- Wrapper Classes in Java
- Java Input Output
- Java AWT Introduction
- Java Layout Manager
- Java Layout Policy
- Java AWT Events
- Collection Framework
- Collection Framework List Interface
- Swing in Java
- Swing Utility Classes
- Swing Layout Managers
- Java JDBC
- Hibernate Framework Overview – Architecture and Basics
Springboot
- Spring Environment Setup
- Spring Boot CRUD REST API Project using IntelliJ IDEA | Postman | MySQL
- Dockerizing Spring Boot Application | Spring Boot Docker Tutorial
- spring-boot-restapidocumentation with swagger
- Spring Boot HttpClient Overview
- Apache HttpClient POST HTTP Request Example
- Apache HttpClient PUT HTTP Request Example
- Apache HttpClient DELETE HTTP Request Example
- Apache HttpClient HTML Form POST Request Example
- Spring Boot JSP Exampl
- Deploying Spring Boot WAR file with JSP to Tomcat
- Spring Boot Annotations
- Spring Core Annotations
- Spring MVC Annotations with Examples
- Spring Scheduling Annotations
- Spring - Java-based Container Configuration
- Spring Java Based Configuration Example
Hibernate
- Hibernate 5 hello world
- Hibernate- One to One Unidirectional Mapping Annotation Example
- Hibernate - Batch Processing
- Hibernate - Interceptors
- Hibernate 5 - Create, Read, Update and Delete (CRUD) Operations Example
- Hibernate Transaction Management
- Hibernate One to Many Unidirectional Mapping Example
- Hibernate One to Many Bidirectional Mapping Example
- Hibernate Many to Many Annotation Mapping Example
- Hibernate Primary KeyJoin Column
- Hibernate First Level Cache with Example
- Hibernate XML Configuration Example with Maven + Eclipse + MySQL Database
- Hibernate Java Configuration Example
- JPA 2 with Hibernate 5 Bootstrapping Example
- JPA and Hibernate Cascade Types
- Hibernate/JPA - Primary Key Generation
- Hibernate 5 - Enum Type Mapping Example
- Hibernate Component Mapping
- Hibernate Object States – Transient,Persistent and Detached
- Hibernate 5 - Save an Entity Example
- Hibernate 5 - Persist an Entity Example
- Hibernate 5 - saveOrUpdate() Method Example
- Hibernate 5 - get(), load() and byId() Method Examples
- Hibernate 5 - merge() Example
- Hibernate 5 - Delete or Remove an Entity Example
- Hibernate 5 - load() Method Example
- Hibernate Session Interface Methods
- Hibernate Session.clear() Method Example
- Introduction Of Java strutes to Architecture
- Struts 2 - Architecture
- Struts 2 - Configuration Files
- Struts 2 - Actions
- Struts 2 - Interceptors
- Struts 2 - Results & Result Types
- Struts 2 - Value Stack/OGNL
- Struts 2 - File Uploading
- Struts 2 - Database Access
- Struts 2 - Validations Framework
JAVA FX
- JavaFX Tutorial
- Introduction to JavaFX Pane
- JavaFX Popup
- JavaFX group
- JavaFX Controller
- JavaFX Gradient Color
- JavaFXAnchorPane
- JavaFXTabPane
- JavaFX Scene
- JavaFX Stage
- JavaFXWebView
- JavaFX Timeline
- JavaFX Timer
- JavaFX Image
- JavaFX Background
- JavaFX dialog
- JavaFX Font
- JavaFXTextArea
- JavaFXObservableList
- JavaFX GUI
- JavaFX FXML
- JavaFXEventHandler
- JavaFXGradle
- JavafxScrollpane
- JavaFXAPI
Spring Boot REST API Documentation with Swagger
In this tutorial, we will learn how to add step-by-step Swagger to the existing Spring boot application and generate Swagger API documentation.
In this tutorial, we’ll look at Swagger 2 for a Spring Boot REST web service, using the Springfox implementation of the Swagger 2 specification
Step 1: Adding the Maven Dependency to Spring Boot Project
Let’s open pom.xml and below single maven springfox-boot-starter dependency:
< dependency> < groupId>io.springfox< /groupId> < artifactId>springfox-boot-starter< /artifactId> < version>3.0.0< /version> < /dependency>
After adding the springfox-boot-starter dependency, spring boot will auto-configure Swagger in Spring boot so we don’t need to manually add annotations to enable Swagger.
Access Swagger documentation JSON API
Here is the URL format:
http://localhost:8080/your-app-root/v2/api-docs
Consider we are creating blog app and to verify that Springfox is working, we can visit this URL in our browser:
http://localhost:8080/blog-app/v2/api-docs
Step 2: Integrating Swagger 2 Into the Spring Boot Project
Let’s create a SwaggerConfig class and annotate with @Configuration annotation. The configuration of Swagger mainly centers around the Docket bean so let’s add the below code to SwaggerConfig class:
importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importspringfox.documentation.builders.PathSelectors; importspringfox.documentation.builders.RequestHandlerSelectors; importspringfox.documentation.service.*; importspringfox.documentation.spi.DocumentationType; importspringfox.documentation.spi.service.contexts.SecurityContext; importspringfox.documentation.spring.web.plugins.Docket; importjava.util.Arrays; importjava.util.Collections; importjava.util.List; @Configuration public class SwaggerConfiguration { privateApiInfoapiInfo() { return new ApiInfo("Blog REST APIs", "REST APIs for Blog Application", "1.0", "Terms of service", new Contact("Ramesh Fadatare", "www.javaguides.net", "ramesh@gmail.com"), "License of API", "API license URL", Collections.emptyList()); } @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } }
After defining the Docket bean, it’s select() method returns an instance of ApiSelectorBuilder, which provides a way to control the endpoints exposed by Swagger.
We can configure predicates for selecting RequestHandlers with the help of RequestHandlerSelectors and PathSelectors. Using any() for both will make documentation for our entire API available through Swagger.
Step 3: Accessing Swagger UI
Swagger UI is a built-in solution that makes user interaction with the Swagger-generated API documentation much easier.
Let’s say we are creating a blog application so in our case, the exact URL will be:
http://localhost:8080/blog-app/swagger-ui/



Customizing Swagger REST Documentation with Annotations
In order to customize the Swagger documentation, swagger-core offers a set of annotations to declare and manipulate the output.
You can find more details about swagger annotations on https://github.com/swagger-api/swagger-core/wiki/annotations
Below code snippet shows how to use @Api and @ApiOperation annotations to provide additional information to REST APIs:
@Api(value = "CRUD Rest APIs for Post resources") @RestController @RequestMapping() publicclassPostController{ privatePostServicepostService; publicPostController(PostServicepostService) { this.postService = postService; } @ApiOperation(value = "Create Post REST API") @PostMapping("/api/v1/posts") publicResponseEntity< PostDto>createPost(@Valid@RequestBodyPostDtopostDto){ returnnewResponseEntity<>(postService.createPost(postDto), HttpStatus.CREATED); } @ApiOperation(value = "Get All Posts REST API") @GetMapping("/api/v1/posts") publicPostResponsegetAllPosts( @RequestParam(value = "pageNo", defaultValue = AppConstants.DEFAULT_PAGE_NUMBER, required = false) intpageNo, @RequestParam(value = "pageSize", defaultValue = AppConstants.DEFAULT_PAGE_SIZE, required = false) intpageSize, @RequestParam(value = "sortBy", defaultValue = AppConstants.DEFAULT_SORT_BY, required = false) StringsortBy, @RequestParam(value = "sortDir", defaultValue = AppConstants.DEFAULT_SORT_DIRECTION, required = false) StringsortDir ){ returnpostService.getAllPosts(pageNo, pageSize, sortBy, sortDir); } @ApiOperation(value = "Get Post By Id REST API") @GetMapping(value = "/api/v1/posts/{id}") publicResponseEntity< PostDto>getPostByIdV1(@PathVariable(name = "id") long id){ returnResponseEntity.ok(postService.getPostById(id)); } @ApiOperation(value = "Update Post By Id REST API") @PutMapping("/api/v1/posts/{id}") publicResponseEntity< PostDto>updatePost(@Valid@RequestBodyPostDtopostDto, @PathVariable(name = "id") long id){ PostDtopostResponse = postService.updatePost(postDto, id); returnnewResponseEntity<>(postResponse, HttpStatus.OK); } @ApiOperation(value = "Delete Post By Id REST API") @DeleteMapping("/api/v1/posts/{id}") publicResponseEntity< String>deletePost(@PathVariable(name = "id") long id){ postService.deletePostById(id); returnnewResponseEntity<>("Post entity deleted successfully.", HttpStatus.OK); } } Below code snippet shows how to use @ApiModel and @ApiModelProperty annotations to provide additional information about Swagger models: @ApiModel(description = "Post model information") @Data public class PostDto { @ApiModelProperty(value = "Blog post id") private long id; // title should not be null or empty // title should have at least 2 characters @ApiModelProperty(value = "Blog post title") @NotEmpty @Size(min = 2, message = "Post title should have at least 2 characters") private String title; // post description should be not null or empty // post description should have at least 10 characters @ApiModelProperty(value = "Blog post description") @NotEmpty @Size(min = 10, message = "Post description should have at least 10 characters") private String description; // post content should not be null or empty @ApiModelProperty(value = "Blog post conent") @NotEmpty private String content; @ApiModelProperty(value = "Blog post comments") private Set< CommentDto> comments; }
Apply now for Advanced Java Training Course