Showing posts with label Quarkus. Show all posts
Showing posts with label Quarkus. Show all posts

Unsupported Media Type error in Quarkus

Unsupported Media Type error in Quarkus

Purpose: In this post, we will learn how we can resolve the given below error.

Error:

Could not find MessageBodyWriter for response object of type: java.util.Collections$SetFromMap of media type: text/html;charset=UTF-8

Next Step. If you have given below project dependency. Please remove this and add the given below.

 
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-resteasy</artifactId>
    </dependency>
Remove above dependency if present and add below.

 
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-resteasy-jackson</artifactId>
    </dependency>

Test API.



Download Code from GitHub.  Source Code




how to enable swagger in quarkus examples

                         Enable swagger in quarkus examples

Purpose: In this post, we will learn how we can enable swagger in Quarkus and test API. The steps are given below.

1. Setup and Create Project: Visit hereCreate Quarkus Hello World Examples.

Next Step. Quarkus Parent Dependencies.

<properties>
        <compiler-plugin.version>3.8.1</compiler-plugin.version>
        <maven.compiler.parameters>true</maven.compiler.parameters>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
        <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
        <quarkus.platform.version>2.2.3.Final</quarkus.platform.version>
        <surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>${quarkus.platform.group-id}</groupId>
                <artifactId>${quarkus.platform.artifact-id}</artifactId>
                <version>${quarkus.platform.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Next Step. Project Dependencies.

       <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-arc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
Next Step. Swagger Dependencies.

        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-smallrye-openapi</artifactId>
        </dependency>
Next Step. Create model.

package org.bce;

public class Employee {

    String id;
    String name;

    public Employee() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Employee(String idString name) {
        super();
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

}

Next Step. Create Employee resources.

package org.bce;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Set;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

@Path("/employees")
public class EmployeeResource {

    private Set<Employeeemployees = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));

    @GET
    public Set<Employeelist() {
        return employees;
    }

    @POST
    public Set<Employeeadd(Employee employee) {
        employees.add(employee);
        return employees;
    }

    @DELETE
    public Set<Employeedelete(Employee employee) {
        employees.removeIf(existingEmployee -> existingEmployee.name.contentEquals(employee.name));
        return employees;
    }
}
Next Step. Server Port:  The default server port is 8080. Wish you want to change open application.properties and add the below line.

quarkus.http.port=8081

Next Step. Open swagger URL in the browser.Click

Add Record : 
Fetch Records



Download Code from GitHub.  Source Code


Quarkus Hello World application

 Quarkus Hello World application

Purpose: In this post, we will learn how we can create a Hello World Quarkus application. The steps are given below.

1. Setup and Create Project: There is two way to create a new project.

a-: We can create a new project online following the link https://code.quarkus.io/

b-: Creating Using STS

Step: Download STS https://spring.io/tools

Next Step. Open STS and go to the marketplace and search Quarkus plugin.


Next Step. In STS go to File->New->Others then select Quarkus

Next Step. Select project type and define project name, package name etc..



Next Step. Add dependencies as you required.


Project Structure: Your project structure looks like as shown in the below image.


Next Step. Quarkus Parent Dependencies.


    <properties>
        <compiler-plugin.version>3.8.1</compiler-plugin.version>
        <maven.compiler.parameters>true</maven.compiler.parameters>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
        <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
        <quarkus.platform.version>2.2.3.Final</quarkus.platform.version>
        <surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>${quarkus.platform.group-id}</groupId>
                <artifactId>${quarkus.platform.artifact-id}</artifactId>
                <version>${quarkus.platform.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Next Step. Project Dependencies.


       <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-arc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
Next Step. Quarkus HelloWorld Resource


package org.bce;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/hello")
public class HelloWorld {

    @GET
    public String hello() {
        return "Hello Quarkus";
    }
}
Next Step. Server Port:  The default server port is 8080. Wish you want to change open application.properties and add the below line.

quarkus.http.port=8081

Next Step. Run the application. See demo below.



Download Code from GitHub.  Source Code