Different calling forms of REST services with Feign client in Spring Cloud

1. Calling a REST service with Query Params

In Spring Cloud, you can send your query params with org.springframework.cloud.openfeign.SpringQueryMap annotation.

In your back-end code, you should use MultiValueMap and LinkedMultiValueMap. Key must be string.

MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();params.add("Name", "Ilhan");
params.add("Number", 26);
ResponseEntity<String> getRestServiceName1Response= feignClient.postRestServiceName1(params, "HEADER_VALUE");

In your feign client code,

@GetMapping(value = "${rest.service.name1}",consumes = MediaType.APPLICATION_XML_VALUE,produces = MediaType.APPLICATION_XML_VALUE)public ResponseEntity<String> getRestServiceName1(@SpringQueryMap MultiValueMap<String, String> params,@RequestHeader(Constants.HEADER_NAME) String headerValue);

Consumes value will set HTTP Content-Type field of your request. Produces value will set HTTP Accept field of your request. With Content-Type field, you say content type of request body . With Accept field, you say content type of response body which you will be able to consume. Value of PostMapping|GetMapping|etc may be loaded from external properties.

2. Calling a REST service with string body

@PostMapping(value ="${rest.service.name2}", 
consumes = MediaType.APPLICATION_XML_VALUE,
produces = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<String> postRestServiceName2(
@RequestBody String request,
@RequestHeader(Constants.HEADER_NAME) String headerValue);

3. Calling a REST service with APPLICATION_FORM_URLENCODED_VALUE

@PostMapping(value = "${rest.service.name3}", consumes = APPLICATION_FORM_URLENCODED_VALUE)public ResponseEntity<String> postRestServiceName3(@RequestBody Map<String, ?> params);

You should use Map<String, ?> type param and also org.springframework.web.bind.annotation.RequestBody annotation.

It will continue…

--

--

--

Computer Engineer | Software Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Microservice Architectures — Single Tenant or Multi-Tenant?

The Obstacle Course

Scratch Commands

Download Full Mac Os High Sierra

Pandas — Merging, Joining & Concatenations

Nick Wille October-18 to October-24th CS371 Blog

CSS Flexbox 101: Vertically Align your items Now

Get The Company Logo Of A Work Email Using This API

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Celal Kartal

Celal Kartal

Computer Engineer | Software Developer

More from Medium

What Is Multithreading | Multithreading Programs In Java

Multithreading

Dynamically route to read-only and read-write database instances in spring-boot

Flyway in Practice — How I use in my projects

Spring Cloud Config Server Encryption and Decryption using Git Repository