首页 > 解决方案 > 错误“无法提取响应:没有找到适合响应类型 ... 和内容类型 [application/json] 的 HttpMessageConverter”

问题描述

我正在向另一个以这种格式返回的服务发送 GET 请求:

{
    "data": [
        {
            "email": "eskaferas@gmail.com",
            "firstName": "Seras",
            "lastName": "Meras"
        },
        {
            "email": "Soras@gmail.com",
            "firstName": "Oras",
            "lastName": "Moras"
        },
        {
            "email": "bzbzb@gmail.com",
            "firstName": "hello",
            "lastName": "bye"
        },
        {
            "email": "lrc@gmail.com",
            "firstName": "Seras",
            "lastName": "Meras"
        }
    ],
    "message": "Success"
}

我以这种方式使用 ResponseEntity :

@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 {
            ResponseEntity<ArrayResponsePojo> result = restTemplate.exchange(uri, HttpMethod.GET,null, new ParameterizedTypeReference<ArrayResponsePojo>() {}); 
            return result;

        }
        catch (HttpClientErrorException ex) {
            return ResponseEntity.status(ex.getRawStatusCode()).headers(ex.getResponseHeaders())
                    .body(ex.getResponseBodyAsString());

        }

    }

但是,当发送 GET 请求时,我得到了这个响应:类型定义错误:[简单类型,类 com.tdl.model.ArrayResponsePojo]; 嵌套异常是com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance ofcom.tdl.model.ArrayResponsePojo(no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)\n at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 2, column: 5]",

我不知道为什么我会得到这个例外,因为我确实有构造函数。这是我的 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;
    }

    @JsonCreator
    public ArrayResponsePojo(User[] data, String message) {
        this.data = data;
        this.message = message;
    }

}

我的用户类:

public class User {

     private String email;
     private String firstName;
     private String lastName;

     @JsonCreator
     public User(String email, String firstName, String lastName) {
        super();
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    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;
     }

}

我尝试了没有和使用 ParameterizedTypeReference。任何人都可以帮忙。我不知道如何解决这个问题。

编辑 添加构造函数后,我得到了这个:

"Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdl.model.ArrayResponsePojo] and content type [application/json]".

这个问题的所有其他示例都是处理与 application/json 不同的响应类型。

我还尝试将标头设置为 application/json。

编辑 2

如果我将 responseEntity 类型设置为 String 我能够接收到响应。但是,我需要收到对我的 POJO 课程的回复

编辑 3 我在调试器中发现了一些无法使用 JsonCreators 的警告。然后我将 JsonProperty 添加到构造函数参数(或只是空构造函数),现在它不返回错误但挂起 30 秒左右,然后才返回预期结果。我在这里打开了另一个问题,因为它是一个不同的问题:Resttemplate super slow for GET request

标签: springspring-bootjackson

解决方案


你的两个类都需要一个无参数的构造函数。这就是错误消息告诉您的内容。


推荐阅读