有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JerseyClient webResource的java问题

我是新手。我有一个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参数中有什么内容

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

3)如有任何额外提示,我将不胜感激,因为这是我第一次打电话休息,也是第一次使用球衣


共 (2) 个答案

  1. # 1 楼答案

    修饰类的path参数将是基uri,例如@Path("/"),然后类内的方法将是/example @Path("test")之后特定uri的path参数

  2. # 2 楼答案

    虽然这是一篇老文章,但我想对一些观点发表评论

    1) What goes in the @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
    
    }
    

    因此,您不需要添加

    Path("")

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

    2) webResource is giving me an error that it cannot be resolved when invoked as webResource.accept. I am unclear why.

    基本上是webResource的功能。接受(…)添加可接受的响应媒体类型,如json、xml、文件等。因此,您应该对此进行详细描述

    3) Any additional tips would be appreciated, as this is my first REST call and first time using jersey.

    您可以检查用于REST的框架之一Jersey