简体   繁体   中英

How to make restful calls from server?

Hi i have a restful project and I make rest calls from client side. But for a case I need to make restful calls from server side. How can i do that?

您可以为此使用Apache HttpClient库。

The JAX/RS client libraries may be used in server code.

My blog has more detail, but here's the relevant code. I'm using Apache Wink.

Resource editionResource = libraryClient.resource( 
                 "http://localhost:9085/LibraryWink/library/editions” 
                 );

BookEdition theEdition = new BookEdition( 
          /* title, isbn etc */ 
          );

ClientResponse response = editionResource  
   .contentType(MediaType.APPLICATION_JSON)  
   .accept(MediaType.APPLICATION_JSON)                     
   .post(theEdition);

As mentioned by @Jonas use HttpClient. Actually there is no difference between client and server in this case. Your server is a client of another server.

But be careful:

if you are in Java EE environment you are not expected to open sockets yourself (at least from EJB). The "right" solution is using JCA to connect to other systems. I once implemented JCA adapter: it is not so hard but requires some efforts.

Probably easier solution is to put the code that opens sockets into servlet, eg one servlet turns to another one (running on different server) over HTTP. I think it is not strongly forbidden by Java EE spec.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM