3

I would like to retrieve the return value of this statement in raw JSON string in order to determine mismatching fields in the classes that are annotated with Jackson.

I have checked the following link:

Spring restTemplate get raw json string

However, in the code snippet in the given link did not work for me because I'm sending a GET request to a Mock service that is currently running on in my computer. Mock service successfully works when I call it as the following code snippet. The problem is the mismatching fields. I also should add HttpMethod.GET and httpEntity fields into my call so that the given link's solution does not work for me.

Here is the code snippted that I would like to retrieve its return value as JSON string:

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

The createUrlWithPort function is given below:

private String createURLWithPort(String uri) {
    return "http://localhost:" + port + uri;
}

Below is the code snippet that I have tried but did not work due to missing httpEntity and HttpMethod.GET

 final String response = restTemplate.getForObject("http://localhost:8080/customer/sms/905469006108?StartDate=2019-01-05&EndDate=2020-01-06", String.class);

Below,the error is given when I call the previous code snippet:

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)

I can add any necessary information so please comment if there is any need.

Thanks!

hakansander
  • 337
  • 4
  • 13
  • the core problem is obvious: `Connection refused` from localhost, but the reasons can be various... – xerx593 Mar 18 '20 at 09:41
  • But when I make this call as the first code snippet the connection is not refused and it succesfully returns me the values but some fields are mismatched and added into the else fields (such as additional properties field in a POJO)... – hakansander Mar 18 '20 at 09:43
  • Maybe you have the wrong port? What value has your field port? – MrFroll Mar 18 '20 at 17:09
  • No, I have checked it out. The port is the same, which is 8080 – hakansander Mar 18 '20 at 17:10
  • Have you tried to get some debug log from `RestTemplate` or an underlying web library? Like `logging.level.org.springframework.web.client.RestTemplate=DEBUG`. Please let me know if you will find the root cause. – MrFroll Apr 02 '20 at 10:46

1 Answers1

4

I have solved the problem,

Here is the erroneous part of my previous code snippet.

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

Instead of mapping the response to Sms class, I should have mapped it to String class. Here is the solution that has been worked for me:

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

Hope this will help to someone facing the same issue with me.

hakansander
  • 337
  • 4
  • 13