Search code examples
javajax-rsrad

Stuck at Restful implementation (Jax-RS)


My server side code:-

@ApplicationPath("/externalpartnerws")
public class ExternalPartnerApplication extends javax.ws.rs.core.Application {
    public Set<Class<?>> getClasses() {
        return  new HashSet<Class<?>>() { { add(ExternalPartnerApplicationResource.class); } };
  }
}

@Path(value="/retrievetier2")
public class ExternalPartnerApplicationResource {

  /**
   * public constructor according to JSR-3.1.2 specification.
   */
  public ExternalPartnerApplicationResource() {}

  @GET
  @Path("/bycountry/{distributorId}/{countryCd}")
  // type "text/plain"  
  @Produces("application/xml")
  public String retrieveTier2ByCountry(
  @PathParam("distributorId") String distributorId, 
  @PathParam("countryCd") String countryCd
  ) {
      if(distributorId == null && countryCd == null)
          return null;
      else //Moving logic from Controller to (Business) Model.
          return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><retrieveTier2ByCountry/>";
  }

web.xml

<servlet>
    <description>JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>RestServlet</servlet-name>
    <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
<init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value> 
        com.ibm.drit.lib.extws.ExternalPartnerApplication
    </param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>RestServlet</servlet-name>
    <url-pattern>/retrievetier2/bycountry/*</url-pattern>
</servlet-mapping>

Client:- On Was 7.0 with RAD 8.0.3

RestClient restClient = new RestClient();
Resource resource = restClient.resource("http://localhost:9080/externalpartnerws/retrievetier2/bycountry/distributorId/2/countryCd/2");
resource.contentType(props.getProperty("text/plain"));
resource.accept(props.getProperty("application/xml"));
ClientResponse response = resource.get();
String responseXml = response.getEntity(String.class);   

I am new in Jax-RS and now on a dead-lock condition with small code in Jax-RS.

I am getting

The following error occurred during the invocation of the handlers chain: 404 - Not Found with message ''null'' while processing GET request sent to ......

Am I doing any basic mistake? Last two days I am spending on this. Please let me know if you require anymore information.


Solution

  • Looks like either your path or path template is wrong. In the example you are giving, you are sending request to the following path (relative to your app root):

    /bycountry/distributorId/2/countryCd/2
    (i.e. 5 path segments in total: bycountry, distributorId, 2, countryCd, 2)

    However, the template on your resource says:

    /bycountry/{distributorId}/{countryCd}
    (i.e. only 3 path segments: bycountry, {distributorId}, {countryCd})

    That does not match - so you are getting 404.

    Either you should change the URL you are sending your request to like this:
    /bycountry/2/2

    Or you should change the path template on your resource to this: /bycountry/distributorId/{distributorId}/countryCd/{countryCd}

    Then it should work.