hakansander
hakansander

Reputation: 377

resttemplate.exchange return value in raw JSON string

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!

Upvotes: 4

Views: 4249

Answers (1)

hakansander
hakansander

Reputation: 377

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.

Upvotes: 5

Related Questions