开发者

Create Restful Client Consumer with Tomcat

I am using Apache tomcat 6.0.20 I want to create Client To Consume RESTFul Web Service(using GET)

I know I can do it via the old fashion way with URLConnecti开发者_运维知识库on (regular GET request).

But I wonder is there any way of doing it differently? maybe with Annotations?


I think this article http://www.oracle.com/technetwork/articles/javase/index-137171.html will give you good guidance how to act in both directions.


I'm currently using the API of spring. The connection handling for example is handled already within the RestTemplate class. Have a look to http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#rest-client-access.


Using NetBeans 7 there is the possibility to have RESTFul web services created with a simple wizard (with Jersey API): http://netbeans.org/kb/docs/websvc/rest.html . This approach uses annotations.


In the end I chose to use the JAVA SE API in the old and fashion way:

public void getRestfullMethod(...) throws IOException
  {
        String temp = null;

        //Build the request data.
        StringBuffer buf = new StringBuffer (..)
        buf.append("&system=").append ("someVal");

        String urlStr = buf.toString ();

        //Send the request.
        URL url = new URL (urlStr);
      URLConnection con = url.openConnection();

      //Return the response.
        BufferedReader in = new BufferedReader (new InputStreamReader (con.getInputStream ()));
        String inputLine = null;

        buf = new StringBuffer ();
        while ((inputLine = in.readLine ()) != null)
              buf.append (inputLine);
        in.close ();

  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜