2

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));
    }
}
Nicolas Filotto
  • 39,066
  • 11
  • 82
  • 105
Ridhi
  • 105
  • 7

1 Answers1

3

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>
Nicolas Filotto
  • 39,066
  • 11
  • 82
  • 105
  • @Nicholas - If I am using Tomcat application server, will jersey-client dependency as mentioned above work ? – Ridhi Jun 04 '16 at 16:23
  • @Ridhi if you use Tomcat, you can verify it on localhost with the jersey client code. I successfully compiled and run your code with the Maven dependencies shown in this answer, and it tries to connect as expected. – mjn Jun 04 '16 at 16:28
  • yes sir, but it won't be provided as Tomcat is not really an application server but more a servlet engine, you will have to put the libs into tomcat/lib – Nicolas Filotto Jun 04 '16 at 16:31