org.springframework.web.client.HttpClientErrorException: 400 错误请求使用 HttpEntity restTemplate.exchange
org.springframework.web.client.HttpClientErrorException: 400 Bad Request using HttpEntity restTemplate.exchange
我在向客户端发布一些数据时收到 HttpClientErrorException: 400 Bad Request api。
异常堆栈跟踪:
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)
代码
模式 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;
}
}
模型 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;
}
}
客户端代码:调用客户端的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();
请求构造以某种方式被破坏。你能帮忙吗?
我在模型 class 中没有任何 json/xml 相关的注释,在 spring-context.xml 中也没有 json/spring rest 相关数据。那是问题吗?
我们正在使用 Spring 4.3.0
客户需要 json 数据,他们没有相同的 UserApiResponseBean.java class。
仅添加转换器名称是不够的。确保 Jackson2 存在于您的类路径中
杰克逊 2.x – com.fasterxml.jackson.databind
如果使用 Maven
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
我在向客户端发布一些数据时收到 HttpClientErrorException: 400 Bad Request api。
异常堆栈跟踪:
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)
代码
模式 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;
}
}
模型 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;
}
}
客户端代码:调用客户端的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();
请求构造以某种方式被破坏。你能帮忙吗? 我在模型 class 中没有任何 json/xml 相关的注释,在 spring-context.xml 中也没有 json/spring rest 相关数据。那是问题吗? 我们正在使用 Spring 4.3.0
客户需要 json 数据,他们没有相同的 UserApiResponseBean.java class。
仅添加转换器名称是不够的。确保 Jackson2 存在于您的类路径中
杰克逊 2.x – com.fasterxml.jackson.databind
如果使用 Maven
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>