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…