Sunday, 3 April 2022

Spring: oepnapi: Document allowable values of a request header

Using schema attribute of @Parameter annotation, we can specify the allowable values of a header.

 

Example

@Parameter(name = "infoLevel", in = ParameterIn.HEADER, description = "info level", required = true, schema = @Schema(type = "string", allowableValues = {
		"ONLY_ID", "ONLYE_NAME_AND_ID", "ALL" }))
@GetMapping("/about-me")
public ResponseEntity<Map<String, Object>> aboutMe() {
	......
	......
}

Above snippet generate below documentation for the header infoLevel.


 
 

Find the below working application.

 

Step 1: Create new maven project ‘openapi-document-header-allowable-values’.

 

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-header-allowable-values</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 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.RestController;

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

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

	@Parameter(name = "authorization1", in = ParameterIn.HEADER, description = "Bearer token", required = true, examples = {
			@ExampleObject(name = "example1", value = "Bearer 1233rada"),
			@ExampleObject(name = "example2", value = "Bearer ASDSFA") })
	@Parameter(name = "infoLevel", in = ParameterIn.HEADER, description = "info level", required = true, schema = @Schema(type = "string", allowableValues = {
			"ONLY_ID", "ONLYE_NAME_AND_ID", "ALL" }))
	@GetMapping("/about-me")
	public ResponseEntity<Map<String, Object>> aboutMe() {
		Map<String, Object> myDetails = new HashMap<>();
		myDetails.put("id", 1);
		myDetails.put("name", "Krishna");

		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 below link.

https://github.com/harikrishna553/springboot/tree/master/openapi/openapi-document-header-allowable-values

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment