Thursday, 31 March 2022

Spring: openapi: Document request headers

Using @Parameter annotation, we can document the headers.

 

Example

@Parameter(name = "authorization1", in = ParameterIn.HEADER, description = "Bearer token", required = true,
examples = {@ExampleObject(name="example1", value="Bearer 1233rada"), @ExampleObject(name="example2", value="Bearer ASDSFA")})
@GetMapping("/about-me")
public ResponseEntity<Map<String, Object>> aboutMe() {
	.....
	.....
}

Above snippet generates below openAPI documentation.


 

Find the below working application.

 

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

 

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</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.

 

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

 

You can download complete working application from this link.



 

Previous                                                    Next                                                    Home

What is sorting phase in MapReduce?

Sorting is a phase that is done on Reducer machine, where all the <key, value> pairs are sorted and group by key before reducer start processing.

What is the advantage of sorting phase?

Since sorting phase sort and groups reducer inputs by keys, it is easy for the reducer to perform aggregate operations.

 

Before Sorting

(day, 2)
(good, 2)
(there, 1)
(the, 1)
(there, 2)
(good, 3)

 

After sorting above data is transformed like below.

(day, [2])
(good, [2, 3])
(the, [1])
(there, [1, 1])

 

What is the order of mapper, partition, shuffling, sorting and reducer phases?

Mapper -> Partitioner -> Shuffle -> Sort -> Reducer

 


Is the sorting phase done on reducer machine?

Yes

 

Previous                                                 Next                                                 Home

Wednesday, 30 March 2022

What is shuffling in MapReduce?

Shuffling is the process of transferring the data from mapper machine to the reducer machine.

 


What is the order of mapper, partition, shuffling, sorting and reducer phases?

Mapper -> Partitioner -> Shuffle -> Sort -> Reducer

 


Is shuffling process occur on MapOnly Job?

Shuffling do not occur when the number of reducers are set to 0.

 

Previous                                                 Next                                                 Home

Spring: OpenAPI: Customize API Documentation path

By defining following application property, we can customize the api documentation path.

 

springdoc.swagger-ui.path=/swagger-ui-app.html

 

Find the below working application.

 

Step 1: Create new maven project ‘openapi-customize-doc-path’.

 

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-customize-doc-path</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: Create application.yml under src/main/resources folder.

 

application.yml

 

springdoc:
  swagger-ui.path: /swagger-ui-app.html

 

Step 4: Define Employee model class.

 

Employee.java

 

package com.sample.app.model;

public class Employee {
	private int id;
	private String firstName;
	private String lastName;
	
	public Employee() {}

	public Employee(int id, String firstName, String lastName) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=")
				.append(lastName).append("]");
		return builder.toString();
	}

}

 

Step 5: Define EmployeeRepository.

 

EmployeeRepository.java

package com.sample.app.repository;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.sample.app.model.Employee;

@Repository
public class EmployeeRepository {

	private static final List<Employee> emps = new ArrayList<>();

	static {
		Employee emp1 = new Employee(1, "Sunil", "Dayanand");
		Employee emp2 = new Employee(2, "Keerthi", "Shetty");
		Employee emp3 = new Employee(3, "Ram", "Anand");

		emps.add(emp1);
		emps.add(emp2);
		emps.add(emp3);

	}

	public List<Employee> all() {
		return emps;
	}

	public Employee add(Employee emp) {
		emps.add(emp);
		return emp;
	}

}

 

Step 6: Define EmployeeController.

 

EmployeeController.java

 

package com.sample.app.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sample.app.model.Employee;
import com.sample.app.repository.EmployeeRepository;

@RestController
@RequestMapping("api/v1/employees")
public class EmployeeController {

	@Autowired
	private EmployeeRepository empRepo;

	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<List<Employee>> all() {
		return ResponseEntity.ok(empRepo.all());
	}

	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Employee> create(@RequestBody Employee emp) {
		Employee persistedEmp = empRepo.add(emp);

		return ResponseEntity.status(HttpStatus.CREATED).body(persistedEmp);

	}
}

 

Step 7: 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-app.html’ in browser to view the documentation.

 

You can download working application from this link.



Previous                                                    Next                                                    Home