Custom encoder

Let's create a custom encoder for a request body.

The request body data has been sent to the server by a POST method using either the String or byte[] parameter. You can add a Content-Type header:

interface AccountService { 
   @PostMapping("/account/") 
   @Headers("Content-Type: application/json") 
   Account create(@RequestBody Account account); 
} 

Let's configure your own custom encoder; now it will be the type-safe request body. Let's see the following example using the feign-gson extension:

class Account { 
    Integer accountId; 
   Double balance; 
   Integer customerId; 
   String accountType; 
   String branchCode; 
   String bank; 
 
public Account(Integer accountId, Double balance, Integer customerId, String accountType, String branchCode, 
               String bank) { 
         super(); 
         this.accountId = accountId; 
         this.balance = balance; 
         this.customerId = customerId; 
         this.accountType = accountType; 
         this.branchCode = branchCode; 
         this.bank = bank; 
   } 
   ... 
} 
 
interface AccountService { 
   @PostMapping("/account/") 
   @Headers("Content-Type: application/json") 
   Account create(@RequestBody Account account); 
} 
... 
AccountService client = Feign.builder() 
                          .encoder(new GsonEncoder()) 
                          .target(AccountService.class, 
"http://ACCOUNT_SERVICE"); client.create(new Account(1001, 2304.32, 100, 'SAVING', 'HDFC0011', 'HDFC'));
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset