Spring Boot Rest API Test with Open-UI Examples
Purpose: In this post, we will learn how we can enable open-ui in spring boot and test API. The steps are given below.
1. Setup and Create Project: Visit here. Spring Boot Rest API Hello World Examples.
Next Step. Spring Boot Parent Dependencies.
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.M1</version><relativePath /> <!-- lookup parent from repository --></parent>
Next Step. Open UI Dependencies.
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-ui</artifactId><version>1.6.4</version></dependency>
Next Step. Spring Boot main class.
Class: Application.java
Note: @SpringBootApplication=@Configuration+ @EnableAutoConfiguration+ @ComponentScan.
package com.bce;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
Next Step. Create Rest Controller.
package com.bce.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloWorldRestController {@GetMappingpublic String hello() {return "Hello World";}}
Next Step. Server Port: The default server port is 8080. Wish you want to change open application.properties and add the below line.
server.port=8081
Next Step. Run the application without swagger open-ui. See demo below.
Next Step: Test with swagger open ui. Open given below URL on the browser.
Download Code from GitHub. Download
No comments:
Post a Comment