如何解决试图在两个服务之间传递 DTO,但得到异常:没有合适的 HttpMessageConverter虽然显然有
我有两个服务(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。所以我看不出问题出在哪里。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。