如何使用模拟服务器返回带有字节数组字段的自定义对象?



我正在使用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)));

但是我的休息模板得到:

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

问题出在哪里??

您不应该直接使用 pdf 文件并将其作为字节数组放入对象中以响应,因为模拟服务器会将application/octet-stream添加为内容类型。 您应该将文件读取为字符串,其结构是手动创建对象的返回实例

试试这个:

final String rString = IOUtils.resourceToString("/your-file.json", StandardCharsets.UTF_8);

new MockServerClient("localhost", 8080)
.when(
request()
.withPath(path)
.withMethod("GET")
)
.respond(
response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withReasonPhrase(HttpStatusCode.OK_200.reasonPhrase())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(rString));

文件应该是这样的

{
"correspondence": {
"correspondenceId": "I am a mock file!",
"correspondenceFileName": "mock-name.pdf",
"correspondenceContent": "YXBwbGljYXRpb24taWQ="
}
}

最新更新