最新编辑
经过对许多不同服务的多次测试后,我怀疑挂起是标头的原因。也许是内容长度标头。但是我不知道为什么它会这样。我非常需要建议。我尝试使用返回较少标头的服务,但它不会挂起。
我有一个奇怪的问题。我正在使用 Resttemplate 通过 GET 请求连接到另一个服务。但是,发送请求后,如果我使用自定义 POJO 作为 ResponseEntity,则需要大约 30 秒才能看到响应。如果我使用 String 作为 ResponseEntity 发送它,它会快速响应(最多 1 秒)。我想知道这可能是什么原因造成的。
响应格式:
{
"data": [
{
"email": "[email protected] /cdn-cgi/l/email-protection",
"firstName": "Seras",
"lastName": "Meras"
},
{
"email": "[email protected] /cdn-cgi/l/email-protection",
"firstName": "Oras",
"lastName": "Moras"
},
{
"email": "[email protected] /cdn-cgi/l/email-protection",
"firstName": "hello",
"lastName": "bye"
},
{
"email": "[email protected] /cdn-cgi/l/email-protection",
"firstName": "Seras",
"lastName": "Meras"
}
],
"message": "Success"
}
My POJO:
public class ArrayResponsePojo {
private User[] data;
private String message;
public User[] getData() {
return data;
}
public void setData(User[] data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ArrayResponsePojo(){
}
}
用户类别:
public class User {
private String email;
private String firstName;
private String lastName;
public User(){
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
获取方法:
@GetMapping("/todos/{toDoNoteId}/users")
public ResponseEntity getNotesUsersTest(@PathVariable int toDoNoteId) {
ToDoNote note = toDoNoteService.getToDoNoteById(toDoNoteId);
if(note==null) {
throw new ToDoNoteNotFoundException("Note with id "+ toDoNoteId + " not found");
}
RestTemplate restTemplate = new RestTemplate();
final String uri = "http://friend:5000/users";
try {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>("parameters", requestHeaders);
ResponseEntity<ArrayResponsePojo> result = restTemplate.exchange(uri, HttpMethod.GET,entity, ArrayResponsePojo.class);
return result;
}
catch (HttpClientErrorException ex) {
return ResponseEntity.status(ex.getRawStatusCode()).headers(ex.getResponseHeaders())
.body(ex.getResponseBodyAsString());
}
}
欢迎任何想法。我非常感谢您的帮助。链接到我之前的问题,也许它是链接的?如果不是速度慢的话,这个问题已经解决了。错误“无法提取响应:没有找到适合响应类型的 HttpMessageConverter ...和内容类型 [application/json]” https://stackoverflow.com/questions/55761194/error-could-not-extract-response-no-suitable-httpmessageconverter-found-for-re
EDIT我注意到它在浏览器中的响应非常快。但我正在使用 Postman 并且使用 Postman 它非常慢。我现在真的迷失了,因为没有抛出任何错误或任何东西。
EDIT 2我从调试器中唯一能看到的是它在方法 afterExecute(task, throwed); 上挂起很长时间;在类 java.util.concurrent.ThreadPoolExecutor 中。这真是令人沮丧:(
EDIT 3我认为这与线程有关,但第二次运行调试时它挂在 java.util.concurrent.LockSupport 类中的另一个方法上,称为
public static void park(Object blocker) {
Thread t = Thread.currentThread();
setBlocker(t, blocker);
UNSAFE.park(false, 0L);
setBlocker(t, null);
}
但我不明白这会需要大约 30 秒,这会出什么问题……实际上花了整整一分钟。为什么如果你在响应实体和restTemplate中传递String而不是我的自定义POJO,响应会正常工作。
另一个编辑我尝试使用curl 获取响应,它获取标头并获取响应,然后挂起一分钟。我不确定那时会发生什么。一分钟后,它退出并显示消息“curl:(18) 传输已关闭,剩余 79 个字节可供读取”
已经尝试设置读取和连接超时没有帮助,尝试连接到不同类型的 POJO 响应的不同服务,它仍然是相同的。