Friday, 8 April 2022

Spring: openAPI: Document query parameters using Operation annotation

Using ‘parameters’ attribute of @Operation annotation, you can document query parameters.

 

Example


@Operation(summary = "about me", description = "Details about myseld", parameters = {
                @Parameter(name = "city", in = ParameterIn.QUERY, 
                description = "city ex: Bangalore", required = true, 
                schema = @Schema(type = "string", allowableValues = {"Bangalore", "Chennai", "Hyderabad" })) })
@GetMapping("/by-city")
public ResponseEntity<Map<String, Object>> infoByName(
                @RequestParam(name = "city", required = false) String firstName) {

        Map<String, Object> usersByCity = new HashMap<>();

        return ResponseEntity.ok(usersByCity);

}

Above snippet generate below swagger documentation.


 

Find the below working application.

 

Step 1: Create new maven project ‘openapi-document-query-parameters-using-operation’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.sample.app</groupId>
        <artifactId>openapi-document-query-parameters-using-operation</artifactId>
        <version>1</version>

        <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.6.4</version>
        </parent>


        <properties>
                <java.version>1.8</java.version>
                <maven.compiler.source>${java.version}</maven.compiler.source>
                <maven.compiler.target>${java.version}</maven.compiler.target>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <project.report.outputEncoding>UTF-8</project.report.outputEncoding>
        </properties>
        
        <dependencies>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-web</artifactId>
                </dependency>


                <dependency>
                        <groupId>org.springdoc</groupId>
                        <artifactId>springdoc-openapi-ui</artifactId>
                        <version>1.6.6</version>
                </dependency>


        </dependencies>
</project>

 

Step 3: Define UserControler class.

 

UserController.java

 

package com.sample.app.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;

@RestController
@RequestMapping(value = "/api/v1/users")
@CrossOrigin("*")
public class UserController {

        @Operation(summary = "about me", description = "Details about myseld", parameters = {
                        @Parameter(name = "city", in = ParameterIn.QUERY, description = "city ex: Bangalore", required = true, schema = @Schema(type = "string", allowableValues = {
                                        "Bangalore", "Chennai", "Hyderabad" })) })
        @GetMapping("/by-city")
        public ResponseEntity<Map<String, Object>> infoByName(
                        @RequestParam(name = "city", required = false) String firstName) {

                Map<String, Object> usersByCity = new HashMap<>();

                return ResponseEntity.ok(usersByCity);

        }

}

 

Step 4: Define main application class.

 

App.java

 

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
        public static void main(String[] args) {

                SpringApplication.run(App.class, args);

        }
}

 

Total project structure looks like below.

 


 

Run App.java.

 

Open the url ‘http://localhost:8080/swagger-ui/index.html’ to experiment with swagger documentation.

 

You can download complete working application from this link.



  

Previous                                                    Next                                                    Home

Spring: openAPI: Document allowable values to the query parameter

By defining the 'schema' attribute of @Parameter annotation, we can document the allowable values to the query parameter.

 

Example

@GetMapping("/by-city")
public ResponseEntity<Map<String, Object>> infoByName(
		@Parameter(name = "city", description = "city ex: Bangalore", required = true, schema = @Schema(type = "string", allowableValues = {"Bangalore", "Chennai","Hyderabad" })) 
		@RequestParam(name = "city", required = false) String firstName) {

		........
		........
}

 

Above snippet generate below documentation.

 



Find the below working application.

 

Step 1: Create new maven project ‘openapi-document-allowable-values-of-query-parameter’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>openapi-document-allowable-values-of-query-parameter</artifactId>
	<version>1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.4</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>


		<dependency>
			<groupId>org.springdoc</groupId>
			<artifactId>springdoc-openapi-ui</artifactId>
			<version>1.6.6</version>
		</dependency>


	</dependencies>

</project>

 

Step 3: Define UserController.

 

UserController.java

 

package com.sample.app.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;

@RestController
@RequestMapping(value = "/api/v1/users")
@CrossOrigin("*")
public class UserController {

	@GetMapping("/by-city")
	public ResponseEntity<Map<String, Object>> infoByName(
			@Parameter(name = "city", description = "city ex: Bangalore", required = true, schema = @Schema(type = "string", allowableValues = {
					"Bangalore", "Chennai",
					"Hyderabad" })) @RequestParam(name = "city", required = false) String firstName) {

		Map<String, Object> usersByCity = new HashMap<>();

		return ResponseEntity.ok(usersByCity);

	}

}

 

Step 4: Define main application class.

 

App.java

 

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {

		SpringApplication.run(App.class, args);

	}
}

 

Total project structure looks like below.

 


 

Run App.java and open the url ‘http://localhost:8080/swagger-ui/index.html’ to see the documentation.

 

You can download complete working application from this link.


 

Previous                                                    Next                                                    Home

How to associate an existing private key to a self-signed certificate?

Below command associate the private key presented in the file ‘myPrivate.key’ to the domain.

openssl req
-key myPrivate.key
-new
-x509 -days 500 -out domain.crt

 

 

Previous                                                    Next                                                    Home

Wednesday, 6 April 2022

Spring: open API: Document query parameter

Using ‘@Parameter’ annotation, we can document the query parameter.

 

Example

public ResponseEntity<Map<String, Object>> infoByName(
		@Parameter(name = "firstName", in = ParameterIn.QUERY, description = "firstName ex: krishna", required = true) 
		@RequestParam(name = "firstName", required = false) String firstName,
		@Parameter(name = "lastName", in = ParameterIn.QUERY, description = "lastName ex: krishna", required = true) 
		@RequestParam(name = "lastName", required = false) String lastName) {

		......
		......
}

Above snippet generates below open api documentation.

  


Find the below working application.

 

Step 1: Create new maven project ‘openapi-document-query-parameter’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>openapi-document-query-parameter</artifactId>
	<version>1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.4</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>


		<dependency>
			<groupId>org.springdoc</groupId>
			<artifactId>springdoc-openapi-ui</artifactId>
			<version>1.6.6</version>
		</dependency>


	</dependencies>

</project>

 

Step 3: Define controller class.

 

UserController.java

 

package com.sample.app.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;

@RestController
@RequestMapping(value = "/api/v1/users")
@CrossOrigin("*")
public class UserController {

	@GetMapping("/by-name")
	public ResponseEntity<Map<String, Object>> infoByName(
			@Parameter(name = "firstName", in = ParameterIn.QUERY, description = "firstName ex: krishna", required = true) @RequestParam(name = "firstName", required = false) String firstName,
			@Parameter(name = "lastName", in = ParameterIn.QUERY, description = "lastName ex: krishna", required = true) @RequestParam(name = "lastName", required = false) String lastName) {

		Map<String, Object> myDetails = new HashMap<>();

		if (firstName != null) {
			myDetails.put("firstName", firstName);
		}

		if (lastName != null) {
			myDetails.put("lastName", lastName);
		}

		return ResponseEntity.ok(myDetails);

	}

}

 

Step 4: Define main application class.

 

App.java

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
	public static void main(String[] args) {

		SpringApplication.run(App.class, args);

	}
}

 

Total project structure looks like below.

 


Run App.java and open the url ‘http://localhost:8080/swagger-ui/index.html’ to see the documentation.

 

You can download complete working application from this link.


 

  

Previous                                                    Next                                                    Home