HttpClientErrorException $ BadRequest:RestTemplate 2.1.4.RELEASE

时间:2019-04-25 18:02:42

标签: spring rest spring-boot resttemplate restful-authentication

我有一个SpringBoot 2.1.4.RELEASE应用程序。使用第三方API。 使用PostmanVersión6.7.4。

我使用以下URL进行身份验证:

https://bonanza.com:7688/pecador/api/v1/auth 并在正文中:{ "username": "nunito.calzada@gmail.com","password": "sdfhhskj$(I$" }

效果很好

我已经实现了这种方法:

protected String authToken (Authentication auth) {

        // Request Header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", "application/json");

        // Request Body
        MultiValueMap<String, String> parametersMap = new LinkedMultiValueMap<String, String>();
        parametersMap.add("username", auth.getName());
        parametersMap.add("password", (String)auth.getCredentials());

        // Request Entity
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(parametersMap, headers);

        // RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // POST Login
        ResponseEntity<String> response = restTemplate.exchange("https://bonanza.com:7688/pecador/api/v1/auth", HttpMethod.POST, requestEntity, String.class);

        HttpHeaders responseHeaders = response.getHeaders();

        List<String> list = responseHeaders.get("Authorization");
        return list == null || list.isEmpty() ? null : list.get(0);
    }

但是我有这个错误:

2019-04-25 19:55  [http-nio-2233-exec-2] ERROR i.i.w.a.e.RestResponseEntityExceptionHandler.handleInternal(95) - 500 Status Code
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 null
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:79)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:579)

我还尝试用auth.getName()补充"nunito.calzada@gmail.com"并用(String)auth.getCredentials()补充"sdfhhskj$(I$",结果相同

用邮递员进行测试,我在标头中设置Content-Type:application / json:

enter image description here

我也用相同的结果尝试了这段代码:

HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", "application/json"); 

        //Request Body 
        MultiValueMap<String, String> parametersMap = new LinkedMultiValueMap<String, String>(); 


          parametersMap.add("username", "ricard.olle@gmail.com");
          parametersMap.add("password", "Iconofcoil100@");


          HttpEntity<?> httpEntity = new HttpEntity<Object>(parametersMap, headers);

          RestTemplate restTemplate = new RestTemplate();

          ResponseEntity<String> response =
                  restTemplate.exchange( BASE_API_URL + "auth", HttpMethod.POST, httpEntity,String.class);


          HttpHeaders responseHeaders = response.getHeaders();

          List<String> list = responseHeaders.get("Authorization"); 

          return list == null || list.isEmpty() ? null : list.get(0);

2 个答案:

答案 0 :(得分:0)

怎么样?

    protected String authToken (Authentication auth) {
        Map<String, String> body = Map.of(
                "username", auth.getName(),
                "password", (String)auth.getCredentials());

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity("https://bonanza.com:7688/pecador/api/v1/auth", body, String.class);
        HttpHeaders responseHeaders = response.getHeaders();

        List<String> list = responseHeaders.get("Authorization");
        return list == null || list.isEmpty() ? null : list.get(0);
    }

答案 1 :(得分:0)

您的行:

// Request Entity
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(parametersMap, headers);

是错误的。 只需要做:

HttpEntity<?> httpEntity = new HttpEntity<Object>(parametersMap, requestHeaders);

因为parametersMap是一个MultiValueMap,但是您还声明了类型为MultiValueMap的HttpEntity,所以这种情况是不正确的。

编辑:

好的,MultiValueMap适用于请求“ MediaType.APPLICATION_FORM_URLENCODED”。您的情况是“ MediaType.APPLICATION_JSON”。 您可以创建一个包装用户名和密码的对象并发送该对象。 例如:

public class ObjectRequest {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

然后:

ObjectRequest obr = new ObjectRequest();
obr.setUsername("nunito.calzada@gmail.com");
obr.setPassword("sdfhhskj$(I$");

并将此对象作为对象放入HttpEntity:

HttpEntity<ObjectRequest> requestEntity = new HttpEntity<ObjectRequest>(obr, headers);

这会将对象自动转换为json。