陷入Restful实施(Jax-RS)

时间:2011-11-13 20:13:48

标签: java jax-rs rad

我的服务器端代码: -

@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>

客户端: - 使用RAD 8.0.3时为7.0;

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);   

我是Jax-RS的新手,现在处于死锁状态,Jax-RS中的代码很小。

我正在

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

我在做任何基本错误吗?最近两天我花在这上面。 如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

看起来您的路径或路径模板错误。在您给出的示例中,您要将请求发送到以下路径(相对于您的应用程序根目录):

/bycountry/distributorId/2/countryCd/2
(即总共5个路径段:bycountry,distributorId,2,countryCd,2)

但是,资源上的模板显示:

/bycountry/{distributorId}/{countryCd}
(即只有3个路径段:bycountry,{distributorId},{countryCd})

这不匹配 - 所以你得到404。

您应该更改发送请求的网址,如下所示:
/bycountry/2/2

或者您应该将资源上的路径模板更改为:/bycountry/distributorId/{distributorId}/countryCd/{countryCd}

然后它应该工作。