JerseyClient webResource问题

时间:2014-08-28 15:00:14

标签: java jersey-client

新手在这里。我有一个看起来像这样的REST服务:

@GET
@PATH("/{id}/headerinfo")
@Produces({ JSON, XML})
public Response getRequestHEADER(@PathParam("id") long id) {
    Request result = em.find(Request.class, id);

    ...

return Response.ok(entity).build();

这是我对它的号召,这给了我一些问题:

@Path("") //what should go here?
public class AaRestCall
    public static String subTrackNum (String trackNum) throws IOException {
        try {
            Client client = Client.create();

            WebResource webResource = client.
            resource("https://url/rest/request/" +   trackNum);

            ClientResponse response = webResource.
            accept("application/json").get(ClientResponse.class);

            String output = response.getEntity(String.class);

            return output;
        }
        catch some stuff here

}

我有几个问题:

1)@Path param会发生什么?

2)webResource给我一个错误,当它被调用为webResource.accept时无法解析。我不清楚为什么。

3)任何其他提示将不胜感激,因为这是我的第一次REST呼叫,并且第一次使用球衣。

2 个答案:

答案 0 :(得分:1)

装饰类的路径参数将是基础uri,例如@Path("/"),然后类中的方法将是/之后的特定uri的路径参数{例如@Path("test")

答案 1 :(得分:0)

虽然这篇文章很老,但我会评论一些观点。

  

1)@Path param会发生什么?

据我所知,下面一个是你的服务器端,我的意思是服务

@GET
@PATH("/{id}/headerinfo")
@Produces({ JSON, XML})
public Response getRequestHEADER(@PathParam("id") long id) {
    Request result = em.find(Request.class, id);

    ...

return Response.ok(entity).build();

然而,第二个是你的客户方;

@Path("") //what should go here?
public class AaRestCall
    public static String subTrackNum (String trackNum) throws IOException {
        try {
            Client client = Client.create();

            WebResource webResource = client.
            resource("https://url/rest/request/" +   trackNum);

            ClientResponse response = webResource.
            accept("application/json").get(ClientResponse.class);

            String output = response.getEntity(String.class);

            return output;
        }
        catch some stuff here

}

因此您无需添加

  

路径( “”)

到您的客户端类或函数。因为客户端请求您正在调用具有其自己的模式URL的API,例如 / service / list ,因此客户端会使用此服务,这意味着不会需要任何路径,除非你没有开发一种提供2 API之间集成的适配器。(这有点不可能的概念,不要太多)。

  

2)webResource给我一个错误,当它无法解决时   作为webResource.accept调用。我不清楚为什么。

webResource.accept(...)的基本功能是添加接受的响应媒体类型,例如json,xml,file等。因此,您应该详细描述。

  

3)任何额外的提示将不胜感激,因为这是我的第一个REST   打电话,第一次使用球衣。

您可以检查用于REST的Jersey

框架之一
相关问题