RESTEasy client for consuming REST APIs

12 篇文章 0 订阅

RESTEasy client for consuming REST APIs

So far in this blog, we have been learning about building RESTful webservices which are server side components. In this post, we will learn to build a RESTful client for consuming the webservices written in previous posts.

I will be re-using the code base written for RESTEasy + JAXB xml example.

The APIs which I will be accessing are as defined.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@GET
@Path ( "/users/{id}" )
public User getUserById ( @PathParam ( "id" ) Integer id)
{
     User user = new User();
     user.setId(id);
     user.setFirstName( "demo" );
     user.setLastName( "user" );
     return user;
}
 
@POST
@Path ( "/users" )
public User addUser()
{
    //Some code
}

To build a RESTful client using client capabilities of JAX-RS RESTEasy, follow given instruction.

1) Verify following RESTEasy dependencies

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- core library -->
< dependency >
     < groupId >org.jboss.resteasy</ groupId >
      < artifactId >resteasy-jaxrs</ artifactId >
     < version >2.3.1.GA</ version >
</ dependency >
< dependency >
     < groupId >net.sf.scannotation</ groupId >
     < artifactId >scannotation</ artifactId >
     < version >1.0.2</ version >
</ dependency >
<!-- JAXB provider -->
< dependency >
     < groupId >org.jboss.resteasy</ groupId >
     < artifactId >resteasy-jaxb-provider</ artifactId >
     < version >2.3.1.GA</ version >
</ dependency >

2) Write the client code to access the GET API as below

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static void sampleResteasyClientGETRequest() throws Exception
{
     //Define the API URI where API will be accessed
     ClientRequest request = new ClientRequest( "http://localhost:8080/RESTfulDemoApplication/user-management/users/10" );
     
     //Set the accept header to tell the accepted response format
     request.accept( "application/xml" );
     
     //RESTEasy client automatically converts the response to desired objects.
     //This is how it is done.
     //Populate the response in user object
     ClientResponse<User> response = request.get(User. class );
     
     //First validate the api status code
     int apiResponseCode = response.getResponseStatus().getStatusCode();
     if (response.getResponseStatus().getStatusCode() != 200 )
     {
         throw new RuntimeException( "Failed with HTTP error code : " + apiResponseCode);
     }
     
     //Get the user object from entity
     User user = response.getEntity();
     
     //verify the user object
     System.out.println(user.getId());
     System.out.println(user.getFirstName());
     System.out.println(user.getLastName());
}

3) Write the client code to access the POST API as below

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static void sampleResteasyClientPostRequest() throws Exception
{
     User user = new User();
     user.setId( 100 );
     user.setFirstName( "Lokesh" );
     user.setLastName( "Gupta" );
     
     StringWriter writer = new StringWriter();
     JAXBContext jaxbContext = JAXBContext.newInstance(User. class );
     Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
     jaxbMarshaller.marshal(user, writer);
     
     //Define the API URI where API will be accessed
     ClientRequest request = new ClientRequest( "http://localhost:8080/RESTfulDemoApplication/user-management/users" );
     
     //Set the accept header to tell the accepted response format
     request.body( "application/xml" , writer.getBuffer().toString());
     
     //Send the request
     ClientResponse response = request.post();
     
     //First validate the api status code
     int apiResponseCode = response.getResponseStatus().getStatusCode();
     if (response.getResponseStatus().getStatusCode() != 201 )
     {
         throw new RuntimeException( "Failed with HTTP error code : " + apiResponseCode);
     }
}
Source code download

Happy Learning !!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值