PrasanthNath PrasanthNath - 1 year ago 242
reST (reStructuredText) Question

Jersey2 Client throwing

I have written a sample REST service using Jersey2.

Here is my web.xml:

<servlet-name>Jersey REST Service</servlet-name>
<servlet-name>Jersey REST Service</servlet-name>

Here is my sample class:

package com.adaequare.resource;


public class Hello {
public String sayHtmlHello(){
return "<html><title>Hello Jersey</title><body><h1>Hello Jersey</h1></body></html>";

public String sayPlainTextHello() {
return "Hello Jersey";

// This method is called if XML is request
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";


I have deployed it to Tomcat and am able to access the following URL:


I tried writing a unit test this way:

package com.adaequare.client;

public class MyResourceTest {
public static final URI BASE_URI = UriBuilder.fromUri("http://localhost").port(8080).build();
private HttpServer server;
private WebTarget target;

public void setUp() throws Exception {

ResourceConfig rc = new ResourceConfig(Hello.class);
server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, rc);

Client c = ClientBuilder.newClient();
target =;

public void tearDown() throws Exception {

public void testGetIt() {
String responseMsg = target.path("jerseysample").path("rest").path("hello").request().get(String.class);
System.out.println("I am here");
assertEquals("Got it!", responseMsg);

This class also throws the exception.

On executing this class, I am getting the following exception:

Exception in thread "main" HTTP 404 Not Found
at org.glassfish.jersey.client.JerseyInvocation.convertToException(
at org.glassfish.jersey.client.JerseyInvocation.translate(
at org.glassfish.jersey.client.JerseyInvocation.access$500(
at org.glassfish.jersey.client.JerseyInvocation$
at org.glassfish.jersey.internal.Errors.process(
at org.glassfish.jersey.internal.Errors.process(
at org.glassfish.jersey.internal.Errors.process(
at org.glassfish.jersey.process.internal.RequestScope.runInScope(
at org.glassfish.jersey.client.JerseyInvocation.invoke(
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(
at com.adaequare.client.TestClient.main(

I am sure I am missing some configuration stuff. I have browsed to see the root cause of the issue but to no avail. Can someone please let me know if I am missing something?

Answer Source

Your service is mapped to (and you are saying you can access it): http://localhost:8080/jerseysample/rest/hello but using your client you are calling http://localhost:8080/restserver/rest/hello which is different URL. What is the surprise?


WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/jerseysample/rest/").path("hello");

As for the second test, try calling getUri() on your WebTarget to see what URL you are actually calling, it should help you see where is the problem.

After your update:

Well first thing is, you haven't specified (in terms of content negotiation) what content your client accepts (you did this in your previous example, which you deleted). But that should not be a problem since in that case server should send you any of implemented ones since by not specifying it you are stating you are supporting all kind of responses. But the problem probably is putting String.class into get() method. There should go an entity you want Jersey to transform the response into. If you want to get String I would do something like this:

Response response = target.path("jerseysample").path("rest").path("hello").
StringWriter responseCopy = new StringWriter();
IOUtils.copy((InputStream) response.getEntity(), responseCopy);

But you can't tell for sure which one of your three method is going to be called since it is on the same PATH, so you should also specify the content by passing it to request method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download