【问题标题】:resttemplate.exchange return value in raw JSON string原始 JSON 字符串中的 resttemplate.exchange 返回值
【发布时间】:2020-06-29 09:01:42
【问题描述】:

我想在原始 JSON 字符串中检索此语句的返回值,以确定使用 Jackson 注释的类中的不匹配字段。

我已经检查了以下链接:

Spring restTemplate get raw json string

但是,给定链接中的代码 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 refused from localhost,但原因可能多种多样……
  • 但是当我将这个调用作为第一个代码 sn-p 时,连接没有被拒绝,它成功地返回了值,但是一些字段不匹配并添加到 else 字段中(例如附加属性字段在 POJO 中)...
  • 也许你的端口有误?你的字段端口有什么价值?
  • 不,我已经检查过了。端口一样,都是8080
  • 您是否尝试过从RestTemplate 或底层网络库获取一些调试日志?喜欢logging.level.org.springframework.web.client.RestTemplate=DEBUG。如果您能找到根本原因,请告诉我。

标签: java spring spring-boot spring-mvc resttemplate


【解决方案1】:

我已经解决了问题,

这是我之前的代码 sn-p 的错误部分。

       ResponseEntity<Sms> smsResponseEntity = restTemplate.exchange(
        createURLWithPort("/customer/sms/905469006108?StartDate=2019-02-05&EndDate=2020-01-06"),
        HttpMethod.GET,
        httpEntity,
        Sms.class);

我不应该将响应映射到 Sms 类,而应该将其映射到 String 类。这是对我有用的解决方案:

       ResponseEntity<String> smsResponseEntity = restTemplate.exchange(
        createURLWithPort("/customer/sms/905469006108?StartDate=2019-02-05&EndDate=2020-01-06"),
        HttpMethod.GET,
        httpEntity,
        String.class);

希望这对与我面临同样问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 2011-03-05
    • 2013-11-29
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2014-06-07
    相关资源
    最近更新 更多