gpt4 book ai didi

java - restTemplate 交换经常会导致 400 错误

转载 作者:行者123 更新时间:2023-12-02 10:27:00 24 4
gpt4 key购买 nike

在我的 Android 应用程序中,我尝试通过restTemplate.exchange 发出 GET 请求,但它经常导致 400 错误,很少是 200 响应。

“http://someURL/items/modified/2018-12-20T12%253A47%253A43%252B01%253A00”的 GET 请求结果为 400 ();调用错误处理程序

org.springframework.web.client.HttpClientErrorException:400

我尝试使用编码和解码参数来执行请求,但这是同样的问题。唯一改变的是请求中的时间戳。我不认为这是后端问题,因为我通过 Swagger 和 Postman 在同一界面上发出了几个请求,并且所有请求都工作正常。我也尝试将 spring-android 更新到版本 2.0.0.M3 但仍然是同样的问题。

String url = ServiceAppConstants.HOSTNAME + ServiceAppConstants.REST_ITEMS_MODIFIED +                             URLEncoder.encode(lastSynchronisationDate);
try {
HttpEntity<String> httpEntity = RestServiceUtils.getHttpEntity(context);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

// runs in the error here
ResponseEntity<ArrayList> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, ArrayList.class);
items= response.getBody();
items = mapper.convertValue(items, new TypeReference<List<Items>>(){});
} catch (RestClientException e) {
/* do stuff */
}

设置 token

@NonNull
public static HttpEntity<String> getHttpEntity(Context context) {
UserStorage userStorage = new UserStorage(context);
HttpHeaders headers = new HttpHeaders();
try {
String token = userStorage.getJsonWebToken();
headers.set(ServiceAppConstants.HEADER_SECURITY_TOKEN, token);
}catch (Exception ex){
Log.e(RestServiceUtils.class.getName(), "Could not get json web token", ex);
}
return new HttpEntity<String>("parameters", headers);
}

这就是请求在 android profiler 中的样子

enter image description here

这是通过 swagger 发送请求时的样子

enter image description here

最佳答案

使用新的 HttpEntity( header ); (没有“参数”)

“parameters”字符串是根据 HttpEntity documentation 的请求正文这可能会导致问题。

关于java - restTemplate 交换经常会导致 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53882722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com