2

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

问题是什么??

4

1 回答 1

1

您不应该直接使用 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="
  }
}
    
于 2020-07-02T18:24:20.237 回答