I am getting HttpClientErrorException: 400 Bad Request when posting some data to client api.
Exception stack trace:
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:498)
Code
Mode class 1: UserApiResponseBean.java
import java.util.List;
public class UserApiResponseBean {
private String userCode;
private String userName;
private List<Department> DepartmentList;
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public List<Department> getDepartmentList() {
return DepartmentList;
}
public void setDepartmentList(List<Department> departmentList) {
DepartmentList = departmentList;
}
}
Model Class 2: Department.java
public class Department {
private String departmentName;
private String businessArea;
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getBusinessArea() {
return businessArea;
}
public void setBusinessArea(String businessArea) {
this.businessArea = businessArea;
}
}
Client Code: Code piece calling client's api
UserApiResponseBean userApiResponseBean = new UserApiResponseBean();
userApiResponseBean.setUserName("userName1");
userApiResponseBean.setUserCode("001");
List<Department> departmentList = new ArrayList<Department>();
Department departmanetData = new Department();
departmanetData.setDepartmentName("Department 1");
departmanetData.setBusinessArea("Business Area 1");
departmentList.add(departmanetData);
userApiResponseBean.setDepartmentList(departmentList);
String userId="userId";
String password="password";
String url = "client api url";
HttpHeaders requestHeader = new HttpHeaders();
String encoding = Base64.getEncoder().encodeToString((userId + ":" + password).getBytes());
requestHeader.add(HttpHeaders.AUTHORIZATION, "Basic " + encoding);
requestHeader.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
requestHeader.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<UserApiResponseBean> requestEntity = new HttpEntity<UserApiResponseBean>(userApiResponseBean, requestHeader);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
String result = responseEntity.getBody();
Somehow request construction is getting corrupted. Can you please help. I do not have any json/xml related annotations in Model class, also there is no json/spring rest related data in spring-context.xml. Is that the problem ? We are using Spring 4.3.0
Client is expecting a json data, they do not have same UserApiResponseBean.java class on their side.
Just adding the converter name isn't enough. Make sure that Jackson2 is present in your classpath
Jackson 2.x – com.fasterxml.jackson.databind
If using Maven
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>