I am using MockServer
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>5.10.0</version>
</dependency>
I'd like to return custom object with which contain many fields
@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;
}
}
The field correspondenceContent
is byte array (file rea it from resource)
This is the mock
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)));
However my restTemplate got:
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)
This is the restTemplate
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();
}
What is the problem??
You should not consume pdf file directly and put it into object to response as a byte array, because mock server will add application/octet-stream
as a content-type.
You should read file as a string with the structure to be return instance of create the object manually
try this:
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));
and the file should be like this
{
"correspondence": {
"correspondenceId": "I am a mock file!",
"correspondenceFileName": "mock-name.pdf",
"correspondenceContent": "YXBwbGljYXRpb24taWQ="
}
}