resttemplate.exchange return 原始 JSON 字符串中的值
resttemplate.exchange return value in raw JSON string
我想在原始 JSON 字符串中检索此语句的 return 值,以确定 类 中用 Jackson 注释的不匹配字段。
我检查了以下内容link:
但是,给定 link 中的代码片段对我不起作用,因为我正在向计算机中当前 运行 上的 Mock 服务发送 GET 请求。当我将其称为以下代码片段时,模拟服务成功运行。问题是字段不匹配。我还应该将 HttpMethod.GET 和 httpEntity 字段添加到我的调用中,以便给定的 link 的解决方案对我不起作用。
这是我想将其 return 值检索为 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
而无法使用的代码片段
final String response = restTemplate.getForObject("http://localhost:8080/customer/sms/905469006108?StartDate=2019-01-05&EndDate=2020-01-06", String.class);
下面,调用前面的代码片段报错:
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.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)
我可以添加任何必要的信息,如果有任何需要,请发表评论。
谢谢!
我已经解决了问题,
这是我之前的代码片段中错误的部分。
ResponseEntity<Sms> smsResponseEntity = restTemplate.exchange(
createURLWithPort("/customer/sms/905469006108?StartDate=2019-02-05&EndDate=2020-01-06"),
HttpMethod.GET,
httpEntity,
Sms.class);
与其将响应映射到 Sms class,不如将其映射到字符串 class。这是对我有用的解决方案:
ResponseEntity<String> smsResponseEntity = restTemplate.exchange(
createURLWithPort("/customer/sms/905469006108?StartDate=2019-02-05&EndDate=2020-01-06"),
HttpMethod.GET,
httpEntity,
String.class);
希望这对遇到与我相同问题的人有所帮助。
我想在原始 JSON 字符串中检索此语句的 return 值,以确定 类 中用 Jackson 注释的不匹配字段。
我检查了以下内容link:
但是,给定 link 中的代码片段对我不起作用,因为我正在向计算机中当前 运行 上的 Mock 服务发送 GET 请求。当我将其称为以下代码片段时,模拟服务成功运行。问题是字段不匹配。我还应该将 HttpMethod.GET 和 httpEntity 字段添加到我的调用中,以便给定的 link 的解决方案对我不起作用。
这是我想将其 return 值检索为 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
而无法使用的代码片段 final String response = restTemplate.getForObject("http://localhost:8080/customer/sms/905469006108?StartDate=2019-01-05&EndDate=2020-01-06", String.class);
下面,调用前面的代码片段报错:
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.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)
我可以添加任何必要的信息,如果有任何需要,请发表评论。
谢谢!
我已经解决了问题,
这是我之前的代码片段中错误的部分。
ResponseEntity<Sms> smsResponseEntity = restTemplate.exchange(
createURLWithPort("/customer/sms/905469006108?StartDate=2019-02-05&EndDate=2020-01-06"),
HttpMethod.GET,
httpEntity,
Sms.class);
与其将响应映射到 Sms class,不如将其映射到字符串 class。这是对我有用的解决方案:
ResponseEntity<String> smsResponseEntity = restTemplate.exchange(
createURLWithPort("/customer/sms/905469006108?StartDate=2019-02-05&EndDate=2020-01-06"),
HttpMethod.GET,
httpEntity,
String.class);
希望这对遇到与我相同问题的人有所帮助。