I do have a spring boot project in which I want to make a external API call using the RestClient which is as below.
Service.java
<code>package com.example.demo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClient;
@org.springframework.stereotype.Service
public class Service {
private final RestClient restClient;
public Service(RestClient restClient) {
this.restClient = restClient;
}
public void callApi() {
String uri = "https://api.appraisd.com/api/users?employeeId=ABC";
String token = "token";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setBearerAuth(token);
httpHeaders.set("Accept", MediaType.APPLICATION_JSON_VALUE);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
Request request = new Request();
request.setFirstName("John");
request.setLastName("Doe");
request.setEmail("[email protected]");
request.setEmployeeId("ABC");
restClient.patch()
.uri(uri)
.headers(headers -> headers.addAll(httpHeaders))
.body(request)
.retrieve()
.body(Response.class);
}
}
</code>
<code>package com.example.demo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClient;
@org.springframework.stereotype.Service
public class Service {
private final RestClient restClient;
public Service(RestClient restClient) {
this.restClient = restClient;
}
public void callApi() {
String uri = "https://api.appraisd.com/api/users?employeeId=ABC";
String token = "token";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setBearerAuth(token);
httpHeaders.set("Accept", MediaType.APPLICATION_JSON_VALUE);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
Request request = new Request();
request.setFirstName("John");
request.setLastName("Doe");
request.setEmail("[email protected]");
request.setEmployeeId("ABC");
restClient.patch()
.uri(uri)
.headers(headers -> headers.addAll(httpHeaders))
.body(request)
.retrieve()
.body(Response.class);
}
}
</code>
package com.example.demo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClient;
@org.springframework.stereotype.Service
public class Service {
private final RestClient restClient;
public Service(RestClient restClient) {
this.restClient = restClient;
}
public void callApi() {
String uri = "https://api.appraisd.com/api/users?employeeId=ABC";
String token = "token";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setBearerAuth(token);
httpHeaders.set("Accept", MediaType.APPLICATION_JSON_VALUE);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
Request request = new Request();
request.setFirstName("John");
request.setLastName("Doe");
request.setEmail("[email protected]");
request.setEmployeeId("ABC");
restClient.patch()
.uri(uri)
.headers(headers -> headers.addAll(httpHeaders))
.body(request)
.retrieve()
.body(Response.class);
}
}
RestClientConfig.java
<code>@Configuration
public class RestClientConfig {
@Bean
public RestClient restClient() {
return RestClient.builder().build();
}
}
</code>
<code>@Configuration
public class RestClientConfig {
@Bean
public RestClient restClient() {
return RestClient.builder().build();
}
}
</code>
@Configuration
public class RestClientConfig {
@Bean
public RestClient restClient() {
return RestClient.builder().build();
}
}
DemoApplication.java
<code>@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
private final Service service;
public DemoApplication(Service service) {
this.service = service;
}
@Override
public void run(String... args) {
service.callApi();
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
</code>
<code>@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
private final Service service;
public DemoApplication(Service service) {
this.service = service;
}
@Override
public void run(String... args) {
service.callApi();
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
</code>
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
private final Service service;
public DemoApplication(Service service) {
this.service = service;
}
@Override
public void run(String... args) {
service.callApi();
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
External API which works fine.
<code>curl --location --request PATCH 'https://api.appraisd.com/api/users?employeeId=ABC'
--header 'Authorization: Bearer token'
--header 'Content-Type: application/json'
--data-raw '{
"firstName":"John",
"lastName":"Doe",
"email":"[email protected]",
"employeeId":"ABC"
}'
</code>
<code>curl --location --request PATCH 'https://api.appraisd.com/api/users?employeeId=ABC'
--header 'Authorization: Bearer token'
--header 'Content-Type: application/json'
--data-raw '{
"firstName":"John",
"lastName":"Doe",
"email":"[email protected]",
"employeeId":"ABC"
}'
</code>
curl --location --request PATCH 'https://api.appraisd.com/api/users?employeeId=ABC'
--header 'Authorization: Bearer token'
--header 'Content-Type: application/json'
--data-raw '{
"firstName":"John",
"lastName":"Doe",
"email":"[email protected]",
"employeeId":"ABC"
}'
When I make a API call using callApi method I do get the following error
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [no body]