Spring boot WireMock在使用SpringBootRestTemplate模拟API调用时始终将响应体设置为null

Spring boot WireMock在使用SpringBootRestTemplate模拟API调用时始终将响应体设置为null,spring-boot,wiremock,Spring Boot,Wiremock,我在wiremock上遇到了一个奇怪的问题。它将响应主体设置为null。如有任何见解,将不胜感激 我的测试中的存根: WireMock.stubFor(post(urlPathEqualTo("http://localhost:8080/mapper")) .willReturn(WireMock.aResponse() .withStatus(HttpStatus.OK.value())

我在wiremock上遇到了一个奇怪的问题。它将响应主体设置为null。如有任何见解,将不胜感激

我的测试中的存根

WireMock.stubFor(post(urlPathEqualTo("http://localhost:8080/mapper"))
            .willReturn(WireMock.aResponse()
                    .withStatus(HttpStatus.OK.value())
                    .withBody(asJson("ct/slotting-response/create_sample_response1.json"))
                    .withHeader("Content-Type","application/json;charset=UTF-8")));
public ResponseEntity<SampleResponse> getsampleValue(final SampleRequest request, RequestHeader requestHeader) throws SlottingException {
    try {
        log.info("Sending request[payload={}]", request);
        final HttpHeaders headers = getRequestHeader(requestHeader);
        HttpEntity<?> entity = new HttpEntity<>(request, headers);
        final ResponseEntity<SampleResponse> response =
                restTemplate.postForEntity("http://localhost:8080/mapper",
                        entity, SampleResponse.class);
        log.info("Sample response {}", response); // response.getBody() gives null
        if (HttpStatus.OK.equals(response.getStatusCode())) {
            log.info("Sample allocated successfully.");
        }
        else {
            throw new SampleException("failed");
        }
        return response;
    }  catch (Exception e) {
        throw new SampleException("Failed", e);
    }
}
使用spring boot resttemplate的实际API调用

WireMock.stubFor(post(urlPathEqualTo("http://localhost:8080/mapper"))
            .willReturn(WireMock.aResponse()
                    .withStatus(HttpStatus.OK.value())
                    .withBody(asJson("ct/slotting-response/create_sample_response1.json"))
                    .withHeader("Content-Type","application/json;charset=UTF-8")));
public ResponseEntity<SampleResponse> getsampleValue(final SampleRequest request, RequestHeader requestHeader) throws SlottingException {
    try {
        log.info("Sending request[payload={}]", request);
        final HttpHeaders headers = getRequestHeader(requestHeader);
        HttpEntity<?> entity = new HttpEntity<>(request, headers);
        final ResponseEntity<SampleResponse> response =
                restTemplate.postForEntity("http://localhost:8080/mapper",
                        entity, SampleResponse.class);
        log.info("Sample response {}", response); // response.getBody() gives null
        if (HttpStatus.OK.equals(response.getStatusCode())) {
            log.info("Sample allocated successfully.");
        }
        else {
            throw new SampleException("failed");
        }
        return response;
    }  catch (Exception e) {
        throw new SampleException("Failed", e);
    }
}
public ResponseEntity getsampleValue(最终SampleRequest请求,RequestHeader RequestHeader)抛出SlottingException{
试一试{
info(“发送请求[payload={}]”,请求);
最终HttpHeaders=getRequestHeader(requestHeader);
HttpEntity=新的HttpEntity(请求、头);
最终反应=
restTemplate.postForEntity(“http://localhost:8080/mapper",
实体,SampleResponse.class);
log.info(“示例响应{}”,response);//response.getBody()给出null
if(HttpStatus.OK.equals(response.getStatusCode())){
log.info(“样本分配成功”);
}
否则{
抛出新的SampleException(“失败”);
}
返回响应;
}捕获(例外e){
抛出新的SampleException(“失败”,e);
}
}
有人能指出你在Wiremock存根中看到的任何明显错误吗


感谢adv

您是否尝试过用stubing代替绝对路径来创建相对路径
.stubFor(post(urlPathEqualTo(“/mapper”))
。我最好的猜测是,这是问题的根源,因此您没有得到与您创建的存根匹配的结果,因为SpringBoot遇到了
http://localhost:8080/mapper
,您正在寻找
http://localhost:8080http://localhost:8080/mapper
作为您的url?我可能在这里偏离了基准……如果这不起作用,我建议您添加有关如何启动WireMock的信息,以及有关您收到的特定错误的更多信息。您是否尝试过使用相对路径而不是绝对路径来存根
.stubFor(post(urlPathEqualTo(“/mapper”))
。我最好的猜测是,这是问题的根源,因此您没有得到与您创建的存根匹配的结果,因为SpringBoot遇到了
http://localhost:8080/mapper
,您正在寻找
http://localhost:8080http://localhost:8080/mapper
作为您的url?我可能在这里偏离了基准……如果这不起作用,我建议添加有关如何启动WireMock的信息,以及有关您收到的特定错误的更多信息。