MicroProfile Rest Client

by Hayri Cicek


Posted on 05-Nov-2018

MicroProfile Rest Client is used to invoke RESTful services over HTTP and in this tutorial we will learn how to use it.

Tools You Will Need
Maven 3.3+
Your favorite IDE. I'm using NetBeans
JDK 1.8+

Thorntail Project Generator
Go to https://thorntail.io/generator/ and follow the steps below to generate a new project.

microprofile thorntail project generator
Enter the Details as Follows
Group ID: com.kodnito
Artifact ID: microprofile-demo
Dependencies: MicroProfile

Click Generate Project to generate and download your project.
Next, Unzip the downloaded zip file and import it into your favorite IDE.

Inside com.kodnito.microprofile.rest package create Todo.java file and add the following :


package com.kodnito.microprofiledemo.rest;

public class Todo {

    private String title;
    private String description;

    public Todo() {
    }

    private Todo(String title, String description) {
        this.title = title;
        this.description = description;
    }

    public static Todo create(String title, String description) {
        return new Todo(title, description);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}


Now in the same package create TodoEndpoint.java file and add the following :


package com.kodnito.microprofiledemo.rest;

import java.util.Arrays;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@ApplicationScoped
@Path("/todos")
public class TodoEndpoint {
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Todo> todos() {
        return Arrays.asList(Todo.create("Buy milk", "Need milk to coffee"), Todo.create("Buy Coffee", "Need Coffee"));
    }
}



Here we created a new endpoint with two Todos.
Inside your project folder enter the following command to start the service.


$ mvn clean package && java -jar target/demo-thorntail.jar -Dswarm.port.offset=1


Open your browser and visit http://localhost:8081/todos to see your todos api.

microprofile restclient json Our service is running and now we will create the second service to invoke the first one with MicroProfile Rest Client.

MicroProfile Rest Client Service
Go to https://thorntail.io/generator/ and follow the steps below to generate a new project.

microprofile restclient thorntail project generator Enter the Details as Follows
Group ID: com.kodnito
Artifact ID: microprofile-restclient
Dependencies: MicroProfile

Click Generate Project and unzip the project and open it in your IDE.
Inside com.kodnito.microprofile.rest package create Todo.java file and add the following :


package com.kodnito.microprofilerestclient.rest;

public class Todo {

    private String title;
    private String description;

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }
}


This file is almost identical to the one we created in the first service and here we only need getters.
Now in the same package create TodoService.java interface and add the following :


package com.kodnito.microprofilerestclient.rest;

import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@ApplicationScoped
@Path("/todos")
public interface TodoService {
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Todo> todos();
}


Here we create an interface with method(s) that represent RESTful APIs endpoint. Now we can use this interface to invoke, the remote service.

Open HelloWorldEndpoint.java and add the following :


@GET
@Path("todos")
@Produces(MediaType.APPLICATION_JSON)
public List todos() throws MalformedURLException {
    URL url = new URL("http://localhost:8081");
    TodoService todoService = RestClientBuilder.newBuilder().baseUrl(url).build(TodoService.class);
    return todoService.todos();
}


When we invoke this endpoint, it will call our remote service and view the todos.
Your HelloWorldEndpoint.java file should look like this :


package com.kodnito.microprofilerestclient.rest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.RestClientBuilder;

@ApplicationScoped
@Path("/hello")
public class HelloWorldEndpoint {

    @GET
    @Produces("text/plain")
    public Response doGet() {
        return Response.ok("Hello from Thorntail REST!").build();
    }

    @GET
    @Path("todos")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Todo> todos() throws MalformedURLException {
        URL url = new URL("http://localhost:8081");
        TodoService todoService = RestClientBuilder.newBuilder().baseUrl(url).build(TodoService.class);
        return todoService.todos();
    }
}


Open a new terminal window and goto the project directory and run the following command to start our second service, but don't stop service one because we will call that service from this service:


$ mvn clean package && java -jar target/demo-thorntail.jar


Now open your browser and visit http://localhost:8080/hello/todos and our service is working.

microprofile restclient


Share this: