0

我有两个服务(Spring Boot 应用程序,在 IDE 中运行)——服务器和客户端。两者都为客户提供相同的 DTO 类。服务器 GET 端点如下所示:

@GetMapping("/api/v1/customer/{customerId}")
public ResponseEntity<CustomerDto> getCustomer(@PathVariable UUID customerId) {
      
    return new CustomerDto(UUID.random(), "TestName");
}

我正在通过restTemplate从客户端向该端点执行请求,期待我的DTO:

String apihost = "http://localhost:8080/api/v1/customer/";
String uuid = UUID.random().toString();
CustomerDto response = restTemplate.getForObject(apihost + uuid, CustomerDto.class);

但是面对这个错误(实际上是异常)

org.springframework.web.client.UnknownContentTypeException: Could not extract response: 
no suitable HttpMessageConverter found for response type 
[class com.keldranase.teafactoryclient.web.model.CustomerDto] 
and content type [application/json]

我很困惑,因为我当然在关注一切正常。客户端显然正在接收 JSON 形式的响应,因为:

Object response = restTemplate.getForEntity(hostapi + uuid, String.class);
System.out.println(response);

// Yields this::
// <200,{"id":"84101676-3363-4be3-a530-0f640b730c8e","name":"TestName"},
// [Content-Type:"application/json", Transfer-Encoding:"chunked", 
// Date:"Thu, 01 Jul 2021 21:13:28 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>

所以我想问题出在转换器的某个地方。我从我的 RestTemplate 中得到了一个转换器列表,并打印了它们:

org.springframework.http.converter.ByteArrayHttpMessageConverter@66908383
org.springframework.http.converter.StringHttpMessageConverter@27e32fe4
org.springframework.http.converter.StringHttpMessageConverter@41477a6d
org.springframework.http.converter.ResourceHttpMessageConverter@2bc12da
org.springframework.http.converter.xml.SourceHttpMessageConverter@3122b117
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@534ca02b
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@29a23c3d

最后一个用于转换 JSON。所以我看不出问题出在哪里。

4

0 回答 0