i am new in Java spring Framework , i need a way to call from my application an external Rest Api. Is there any 'best practice' http client so i can use for my need?
Thanks in Advance
i am new in Java spring Framework , i need a way to call from my application an external Rest Api. Is there any 'best practice' http client so i can use for my need?
Thanks in Advance
Use RestTemplate:
@RestController
public class SampleController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/sample/endpoint", method = RequestMethod.POST)
public String createProducts(@RequestBody SampleClass sampleClass) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<SampleClass> entity = new HttpEntity<SampleClass>(sampleClass,headers);
return restTemplate.exchange(
"https://example.com/endpoint", HttpMethod.POST, entity, String.class).getBody();
}
}