从Restfull服务(客户端应用程序内部)检索实体列表



我是Rest web服务的新手,我说我使用Netbeans 创建了这个web服务

@Path("browse")
@Stateless
public class ArticleBrowseResource {
   @EJB
   private ArticleSearcherLocal ejbRef;
   @GET
   @Produces(MediaType.APPLICATION_XML)
   public List<Article> browse(@DefaultValue("") @QueryParam("username") String username,@QueryParam("sd") String sd) {
      // convert sd string to date
      List<Article> articles = ejbRef.search(username, date);
      return articles;
   }
}

其中Article是与@XmlRootElement 交互的实体

现在,我如何在客户端中检索这一文章列表?为了简单起见,我们只说它是一个java标准应用程序?在SOAPWeb服务中,我知道这些对象是自动生成的,但在Rest.中没有

这是Netbeans 为该服务生成的客户端类

public class ArticleBrowseClient {
  private WebResource webResource;
  private Client client;
  private static final String BASE_URI = "http://localhost:8080/cityblog/rest";
  public ArticleBrowseClient() {
    com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
    client = Client.create(config);
    webResource = client.resource(BASE_URI).path("browse");
  }
  public <T> T browse(Class<T> responseType, String username, String sd) throws UniformInterfaceException {
    WebResource resource = webResource;
    if (username != null) {
        resource = resource.queryParam("username", username);
    }
    if (sd != null) {
        resource = resource.queryParam("sd", sd);
    }
    return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
  }
  public void close() {
    client.destroy();
  }
}

解决这个问题的最佳和最简单的方法是什么?

如有任何帮助,我们将不胜感激thx提前

请尝试减少代码生成,更多地了解您实际在做什么。在服务器上,您可以在JAXB的帮助下生成一条XML消息。在客户端,您可以使用您喜欢的编程语言和库来使用此XML。只需使用curl之类的工具,就可以看到"电线"上到底发生了什么。您生成的客户端站点看起来完全合理。您只需要从客户端的服务器端获得Article类。生成的代码使用Jersey,默认情况下每个JAXB可以读取XML消息。因此,只需将服务器端的Article类放在客户端类路径中并使用它。但也请查看有线级别的协议,以了解REST API的可移植性。

最新更新