org.springframework.web.client.HttpClientErrorException:400使用HttpEntity的错误请求restTemplate.exchange

时间:2018-03-24 22:57:38

标签: java spring post resttemplate httpentity

我在将一些数据发布到客户端api时收到HttpClientErrorException:400 Bad Request。

异常堆栈跟踪:

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: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();

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

客户端期待一个json数据,他们没有相同的UserApiResponseBean.java类。

1 个答案:

答案 0 :(得分:0)

仅添加转换器名称是不够的。确保您的类路径中存在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>