Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Specify the MediaType Consumed by a REST API in Micronaut?

You can specify the media type that is consumed by a REST API in two ways.

 

a. Using @Consumes annotation

@Consumes annotation specify the media types consumed by a particular component.

 

Example

@Post("/multiple1")
@Consumes({ MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON })
public HttpResponsecreate2(@Body final Employee employee) {
	return HttpResponse.status(HttpStatus.CREATED).body(employee);
}

 

b. Using consumes attribute of http methods.

@Post(value = "/multiple2", consumes = {MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON})
public HttpResponse create3(@Body final Employee employee) {
	return HttpResponse.status(HttpStatus.CREATED).body(employee);
}

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-consume-media-type’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

 

xml version="1.0" encoding="UTF-8"?>
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	modelVersion>4.0.0modelVersion>
	groupId>com.sample.appgroupId>
	artifactId>micronaut-consume-media-typeartifactId>
	version>0.1version>
	packaging>jarpackaging>

	parent>
		groupId>io.micronautgroupId>
		artifactId>micronaut-parentartifactId>
		version>3.7.4version>
	parent>

	properties>
		packaging>jarpackaging>
		jdk.version>11jdk.version>
		release.version>11release.version>
		micronaut.version>3.7.3micronaut.version>
		micronaut.runtime>nettymicronaut.runtime>
		exec.mainClass>com.sample.app.Appexec.mainClass>
	properties>

	repositories>
		repository>
			id>centralid>
			url>https://repo.maven.apache.org/maven2url>
		repository>
	repositories>

	dependencies>
		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-injectartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-validationartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-http-clientartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-http-server-nettyartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-jackson-databindartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>jakarta.annotationgroupId>
			artifactId>jakarta.annotation-apiartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>ch.qos.logbackgroupId>
			artifactId>logback-classicartifactId>
			scope>runtimescope>
		dependency>
		dependency>
			groupId>io.micronaut.testgroupId>
			artifactId>micronaut-test-junit5artifactId>
			scope>testscope>
		dependency>
		dependency>
			groupId>org.junit.jupitergroupId>
			artifactId>junit-jupiter-apiartifactId>
			scope>testscope>
		dependency>
		dependency>
			groupId>org.junit.jupitergroupId>
			artifactId>junit-jupiter-engineartifactId>
			scope>testscope>
		dependency>
	dependencies>

	build>
		plugins>
			plugin>
				groupId>io.micronaut.buildgroupId>
				artifactId>micronaut-maven-pluginartifactId>
			plugin>

			plugin>
				groupId>org.apache.maven.pluginsgroupId>
				artifactId>maven-compiler-pluginartifactId>
				configuration>
					
					

					annotationProcessorPaths
						combine.children="append">
						path>
							groupId>io.micronautgroupId>
							artifactId>micronaut-http-validationartifactId>
							version>${micronaut.version}version>
						path>
					annotationProcessorPaths>

				configuration>
			plugin>
		plugins>
	build>

project>

 

Step 3: Define Employee model class.

 

Employee.java
package com.sample.app.model;

public class Employee {

	private Integer age;
	private String name;

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 

Step 4: Define EmployeeController class.

 

EmployeeController.java

package com.sample.app.model;

public class Employee {

	private Integer age;
	private String name;

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 

Step 5: Define main application class.

 

App.java

package com.sample.app;

import io.micronaut.runtime.Micronaut;

public class App {

	public static void main(String[] args) {

		Micronaut.run(App.class);

		// Use this if you want the beans to be initialized eagerly
		/*
		 * Micronaut.build(args) .eagerInitSingletons(true) .mainClass(App.class)
		 * .start();
		 */
	}
}

Total project structure looks like below.


 


 

Build the project using mvn package command.

Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.

 

Upon command successful execution, you can see the jar file ‘micronaut-consume-media-type-0.1.jar’ in project target folder.


$ ls ./target/
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-consume-media-type-0.1.jar
original-micronaut-consume-media-type-0.1.jar
test-classes

 

Execute below command to run the application.

java -jar ./target/micronaut-consume-media-type-0.1.jar

 

Execute below commands to test the apis.

curl --location --request POST 'http://localhost:8080/employees' \
--header 'Content-Type: application/json' \
--data-raw '{
    "age": 28,
    "name": "Krishna"
}'

curl --location --request POST 'http://localhost:8080/employees/multiple1' \
--header 'Content-Type: application/json' \
--data-raw '{
    "age": 28,
    "name": "Krishna"
}'


curl --location --request POST 'http://localhost:8080/employees/multiple1' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'age=28' \
--data-urlencode 'name=krishna'


curl --location --request POST 'http://localhost:8080/employees/multiple2' \
--header 'Content-Type: application/json' \
--data-raw '{
    "age": 28,
    "name": "Krishna"
}'


curl --location --request POST 'http://localhost:8080/employees/multiple2' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'age=28' \
--data-urlencode 'name=krishna'

$curl --location --request POST 'http://localhost:8080/employees' \
> --header 'Content-Type: application/json' \
> --data-raw '{
>     "age": 28,
>     "name": "Krishna"
> }'
{"age":28,"name":"Krishna"}$
$
$
$curl --location --request POST 'http://localhost:8080/employees/multiple1' \
> --header 'Content-Type: application/json' \
> --data-raw '{
>     "age": 28,
>     "name": "Krishna"
> }'
{"age":28,"name":"Krishna"}$
$
$
$curl --location --request POST 'http://localhost:8080/employees/multiple1' \
> --header 'Content-Type: application/x-www-form-urlencoded' \
> --data-urlencode 'age=28' \
> --data-urlencode 'name=krishna'
{"age":28,"name":"krishna"}$
$
$
$curl --location --request POST 'http://localhost:8080/employees/multiple2' \
> --header 'Content-Type: application/json' \
> --data-raw '{
>     "age": 28,
>     "name": "Krishna"
> }'
{"age":28,"name":"Krishna"}$
$
$
$curl --location --request POST 'http://localhost:8080/employees/multiple2' \
> --header 'Content-Type: application/x-www-form-urlencoded' \
> --data-urlencode 'age=28' \
> --data-urlencode 'name=krishna'
{"age":28,"name":"krishna"}

You can download this application from this link.


 

Previous                                                    Next                                                    Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

How to Specify the MediaType Consumed by a REST API in Micronaut?

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×