I am learning to implement restful webservices with Jersey. I always get “POST
http://localhost:8080/rest/welcome/post
returned a response status of 404 Not Found” – What is wrong with my code below? Context root on the server side is rest and I have @Path welcome at class level and @pPath post at method level.
Client
public class WelcomeRestJsonClient {
@Produces("application/json")
@Consumes("text/plain")
public void send() {
MyObject myObject = new MyObject();
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getClasses().add(com.restclient.MyJsonProvider.class);
Client client = Client.create(clientConfig);
WebResource webResource = client.resource("http://localhost:8080/rest/welcome/post");
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, myObject);
System.out.println("success");
}
}
Server
@Path("/welcome")
public class WelcomeRestJson {
@POST
@Path("/post")
@Produces("text/plain")
@Consumes("application/json")
public String processPostData(MyObject myObject) {
System.out.println("Inside processPostData");
return "success";
}
}
Am I facing this problem because of incorrect JsonProvider configuration? On the client side, I am using MyJsonProvider which extends JacksonJaxbJsonProvider to convert MyObject to Json. My code on the server side simply accepts MyObject. Do I need some code to hook up Json provider on server side too?
Here is my web.xml
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
I can access rest index.html. I see the following in the log file.
INFO: Root resource classes found: class com.rest.WelcomeRestJson
I removed “/” with the path. Still same 404 error. Please help.
After adding Jersey Servlet to
web.xml, get with no parameter started working. But post still failed. Adding the following init parameter to Jersey Servlet and replacing the Jersey Servlet mapping from/rest/*to/*fixed the issue.