我正在使用 MockServer
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>5.10.0</version>
</dependency>
我想返回包含许多字段的自定义对象
@AllArgsConstructor(staticName = "of")
static class WcRetrievalResponse implements Serializable {
private final Correspondence correspondence;
@AllArgsConstructor(staticName = "of")
static class Correspondence implements Serializable{
private final String correspondenceId;
private final String correspondenceFileName;
private final byte[] correspondenceContent;
}
}
该字段correspondenceContent
是字节数组(文件从资源中读取)
这是模拟
final byte[] successResponse = IOUtils.resourceToByteArray("/mock-file.pdf");
WcRetrievalResponse response = WcRetrievalResponse.of(WcRetrievalResponse.Correspondence.of("AN_APP_ID", "Mock-file.pdf", successResponse));
new MockServerClient("localhost", 8080)
.when(
request()
.withPath(path)
.withMethod("GET")
)
.respond(
response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withReasonPhrase(HttpStatusCode.OK_200.reasonPhrase())
.withBody(SerializationUtils.serialize(response)));
但是我的 restTemplate 得到了:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class pnc.aop.core.basicsign.boot.wc.impl.WcRetrievalResponse] and content type [application/octet-stream]] with root cause
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class pnc.aop.core.basicsign.boot.wc.impl.WcRetrievalResponse] and content type [application/octet-stream]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:123)
这是其余模板
public byte[] retrieve(String wcId) {
final HttpHeaders headers = new HttpHeaders();
headers.set("serviceId", this.wcServiceProperties.getServiceId());
headers.set("servicePassword", this.wcServiceProperties.getServicePassword());
final HttpEntity<?> entity = new HttpEntity<>(headers);
final ResponseEntity<WcRetrievalResponse> response =
this.restOperations.exchange(URI.create(this.wcServiceProperties.getRetrievalUrl() + wcId), HttpMethod.GET, entity, WcRetrievalResponse.class);
if (response.getBody() == null ||
response.getBody() .getCorrespondence() == null ||
response.getBody() .getCorrespondence().getCorrespondenceContent() == null) {
throw new IllegalStateException("Not acceptable response from WC service: " + response);
}
return response.getBody().getCorrespondence().getCorrespondenceContent();
}
问题是什么??