Ridhi
Ridhi

Reputation: 105

How to write a client for REST web Services?

I am trying to write a client for REST web Services; I am using Tomcat application server. All the below packages are not being supported.

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

I am new to REST, which dependency should I add to pom.xml for these libraries?

package com.abc.client;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

public class JaxRSClient {
    public static void main(String args[]){
        // Creating the client
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(
            "http://localhost:8080/jersey/RestWebService/HelloREST/"
        );
        System.out.println(target.request(MediaType.TEXT_PLAIN).get(String.class));
    }
}

Upvotes: 2

Views: 1054

Answers (1)

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

You have all those classes in javax.ws.rs-api

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0.1</version>
</dependency>

But this is only the API, you will need to choose your JAX-RS implementation such as Jersey (you can get the full list of existing implementations here).

If you want to use Jersey, you will need to add only the next dependency to your pom file, indeed javax.ws.rs-api is already a dependency of jersey-client

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.23</version>
</dependency>

Upvotes: 3

Related Questions