org.springframework.web.client.HttpClientErrorException: 400



我在将一些数据发布到客户端 API 时收到 HttpClientErrorException: 400 错误请求。

异常堆栈跟踪:

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)

法典

模式类 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;
}
}

模型类2:部门.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();

不知何故,请求构造已损坏。你能帮忙吗? 我在模型类中没有任何与 json/xml 相关的注释,在 spring-context.xml 中也没有与 json/spring rest 相关的数据。这是问题所在吗? 我们正在使用Spring 4.3.0

客户端需要 json 数据,它们没有相同的 UserApiResponseBean.java 类。

仅添加转换器名称是不够的。确保 Jackson2 存在于类路径中

Jackson 2.x – com.fasterxml.jackson.databind

如果使用 Maven

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>

相关内容

  • 没有找到相关文章

最新更新