使用jersey客户端访问webservice

时间:2015-04-24 23:19:57

标签: rest jersey webservice-client

package java4s;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/vinay")
public class JsonFromRestful {

@POST
@Path("/{runID}/{tweetID}/{tweet : .*}")
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello(             @PathParam("runID") String rid,
                                             @PathParam("tweetID") String    tid,
                                             @PathParam("tweet") String twt)    throws IOException, InterruptedException {

      StringBuffer resneel=new StringBuffer();

         resneel.append(rid);
         resneel.append(tid);
         resneel.append(twt);
         return return resneel.toString();



    }
    }

 client test program

 package neel;

 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import com.sun.jersey.api.client.Client;
 import com.sun.jersey.api.client.ClientResponse;
 import com.sun.jersey.api.client.WebResource;
 import com.sun.jersey.core.util.MultivaluedMapImpl;

 /*
   * excerpt of the maven dependencies
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.19</version>
    </dependency>        
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-core</artifactId>
    <version>1.19</version>
     </dependency>   
    */

    public class ClientTest {

   public static void main(String[] args)
   {
      if(args.length != 4)
    {
        System.out.println("Incorrect parameters, usage:");
        System.out.println("java -jar neelclienttest.jar run_id tweet_id tweet_to_annotate rest_api");
        System.exit(1);
    }

    String runID = args[0];
    String tweetID = args[1];
    String tweet = args[2];
    String uri = args[3];

    try {
        String annotations = annotate(uri, runID, tweetID, tweet);
        System.out.println(annotations);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public static String annotate(String uri, String runID, String tweetID,    String tweet) throws Exception 
{

    Client client = Client.create();        
    WebResource webResource = client.resource(uri);

    MultivaluedMap<String,String> params =  new MultivaluedMapImpl();
    params.add("RunID", runID);
    params.add("TweetID", tweetID);
    params.add("Tweet", tweet);

    ClientResponse response = webResource.
                                accept(MediaType.TEXT_PLAIN_TYPE). 
                                post(ClientResponse.class, params);

    // check if status code != 200 OK
    if ( response.getStatus() != 200 ) 
        throw new Exception ("The REST interface has answered with an unexpected status code" + response.getStatus());

    return response.getEntity(String.class);

  }
  }

这里我给出命令行争论runid tweetid tweet,路径为http://localhost/cen_neel/vinay/,但它显示了java.lang异常。我们创建了一个Web服务,无论我给客户端测试的是什么。我不需要客户端测试中的任何更改。如果我的程序有任何错误,请提供帮助。我使用下面的restclient插件测试了相同的程序 http://localhost/cen_neel/vinay/r12/v23/Chennai我正在得到正确回复。

1 个答案:

答案 0 :(得分:0)

此处post(ClientResponse.class, params);。您尝试将URI信息发布到请求正文中。那不是他们所属的地方。实际上,根据你的资源方法,根本不应该是一个机构。你可以简单地传递一个空字符串。

相反,您应该如何构建请求,.path()上的WebResource附加路径段

ClientResponse response = webResource
                          .path(runID)
                          .path(tweetID)
                          .path(tweet)
                          .accept(MediaType.TEXT_PLAIN_TYPE) 
                          .post(ClientResponse.class, "");

但是,你做这件事的方式似乎毫无意义。为什么不直接传递整个URI,就像你在Rest Client中一样。

无论如何,这并不是一个好的设计。以下是我要做的一些改进。

  1. 实际的推文不应该在URI中。它应该贴在身上。要在正文中接受它,只需使用@PathParam方法参数对tweet进行注释,然后删除路径模板{tweet : .*}。然后你可以post(ClientReponse.class, tweet)。您还应该将@Consumes(MediaType.TEXT_PLAIN)添加到资源方法,并使用type创建请求。即。

    .accept(MediaType.TEXT_PLAIN_TYPE)
    .type(MediaType.TEXT_PLAIN_TYPE)
    .post(ClientResponse.class, tweet);
    
  2. 如果您尝试创建新推文,则ID不应位于模板中。 id不应该存在。创建推文时,服务器应通过新创建的推文ID通知客户端,并带有Location标题。

相关问题