Reputation: 51
I am trying to learn RESTFul web services using Jersey and following this example. I have created a sample service which is accessible at:
http://localhost:8080/de.vogella.jersey.first/rest/hello.
I have created a client which calls this service but when I run this client I get an exception as follows:
GET http://localhost:8080/de.vogella.jersey.first/rest/hello
returned a response status of 404 Not Found Exception in thread "main"
com.sun.jersey.api.client.UniformInterfaceException:
GET http://localhost:8080/de.vogella.jersey.first/rest/hello
returned a response status of 404 Not Found
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
at de.vogella.jersey.first.client.Test.main(Test.java:23)
Service class is
public class Hello {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
}
Client Code:
public class Test {
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_XML).get(String.class));
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/de.vogella.jersey.first").build();
}
}
The strange part is, I can get the right result if I hit
http://localhost:8080/de.vogella.jersey.first/rest/hello
from browser. Any help to resolve this issue is appreciated. Thanks
Upvotes: 3
Views: 14757
Reputation: 8838
You should use:
http://localhost:8080/hello
instead of
http://localhost:8080/de.vogella.jersey.first/rest/hello
For testing purposes.
Upvotes: 0
Reputation: 1035
URL: de.vogella.jersey.first/rest/hello
1) Make sure that you have given Servlet mappings to parse 'rest' from URL
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
...
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
2) Add @Path("hello")
declaration above method.
@Path("hello")
public String sayXMLHello() {}
Upvotes: 1
Reputation: 11
As per your requirement in your service application you didn't mention the "Path" annotation before you creating the class, For instance:
@Path("hello")
public class Hello {
}
That's only the problem in your service application.
Upvotes: 1
Reputation: 4870
i am not getting any problem in this example if you want to run that example in another way than pass whole url which you pass in browser in following function you'll get result in your main class.
private String doGet(String url){
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource(url);
ClientResponse response = resource.type("application/x-www-form-urlencoded").get(ClientResponse.class);
String en = response.getEntity(String.class);
return en;
}
Upvotes: 0