我的服务器端代码:-

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


我在犯任何基本错误吗?最近两天,我在此上花钱。
如果您需要更多信息,请告诉我。

最佳答案

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

/bycountry/distributorId/2/countryCd/2
(即总共5个路径段:按国家/地区,distributorId,2,countryCd,2)

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

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

那不匹配-因此您得到404。

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

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

然后它应该工作。

关于java - 卡在Restful实现(Jax-RS),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8114388/

10-09 05:06