【发布时间】:2020-06-29 09:01:42
【问题描述】:
我想在原始 JSON 字符串中检索此语句的返回值,以确定使用 Jackson 注释的类中的不匹配字段。
我已经检查了以下链接:
但是,给定链接中的代码 sn-p 对我不起作用,因为我正在向当前在我的计算机上运行的 Mock 服务发送 GET 请求。当我将其称为以下代码 sn-p 时,模拟服务成功运行。问题是不匹配的字段。我还应该在我的调用中添加 HttpMethod.GET 和 httpEntity 字段,以便给定链接的解决方案对我不起作用。
这里是我想以 JSON 字符串形式检索其返回值的代码片段:
ResponseEntity<Sms> smsResponseEntity = restTemplate.exchange(
createURLWithPort("/customer/sms/905469006108?StartDate=2019-02-05&EndDate=2020-01-06"),
HttpMethod.GET,
httpEntity,
Sms.class);
createUrlWithPort 函数如下:
private String createURLWithPort(String uri) {
return "http://localhost:" + port + uri;
}
下面是我尝试过但由于缺少httpEntity和HttpMethod.GET而无法工作的代码sn-p
final String response = restTemplate.getForObject("http://localhost:8080/customer/sms/905469006108?StartDate=2019-01-05&EndDate=2020-01-06", String.class);
下面,我调用之前的代码sn-p时报错:
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8080/customer/sms/905469006108": Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:311)
at org.springframework.boot.test.web.client.TestRestTemplate.getForObject(TestRestTemplate.java:214)
at com.vodafone.customer.CustomerIntegrationTest.testRetrieveSmsDataBetweenDates_ReturnsHttp200(CustomerIntegrationTest.java:54)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
我可以添加任何必要的信息,如果有任何需要,请发表评论。
谢谢!
【问题讨论】:
-
核心问题很明显:
Connection refusedfrom localhost,但原因可能多种多样…… -
但是当我将这个调用作为第一个代码 sn-p 时,连接没有被拒绝,它成功地返回了值,但是一些字段不匹配并添加到 else 字段中(例如附加属性字段在 POJO 中)...
-
也许你的端口有误?你的字段端口有什么价值?
-
不,我已经检查过了。端口一样,都是8080
-
您是否尝试过从
RestTemplate或底层网络库获取一些调试日志?喜欢logging.level.org.springframework.web.client.RestTemplate=DEBUG。如果您能找到根本原因,请告诉我。
标签: java spring spring-boot spring-mvc resttemplate