HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Making a call to a server running a RESTful service

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
callrunningservicerestfulservermaking

Problem

I am working on a project in which I am making a call to one of my servers using RestTemplate which is running a RESTful service and getting the response back from them.

The response that I will be getting from my server can be either of these error responses (that's all I have for error response) if something has gone wrong:

{"warning": "user_id not found", "user_id": some_user_id}
{"error": "user_id for wrong partition", "user_id": some_user_id, "partition": some_partition}
{"error": "missing client id", "client_id":2000}


or below successful response if there is no failures (it can be any random json string, key can be different as well) -

{"@data": {"oo":"1205000384","p":"2047935"}}


  • If I am getting any error response as mentioned above, then I am deserializing it so that I can log them as an error with a specific error or warning I got front the server which can be for example - user_id not found or missing client id.



  • If it is a successful response, then I am also deserializing it, which I don't need for my use case as we don't have any POJO. I just need to return the response as is which I receive from the server.



In my use case, I don't need to deserialize my response string if it is a successful response as we don't have any POJO for that and we are returning the response string as it is which we have got from the server. But just for logging specific error messages (if I am getting error response from the server) I am deserializing it which I am thinking is unnecessary. There might be better solution for my use case.

Here is my Java client which is calling a Callable task using future.get:

```
public class TestingClient implements IClient {

private ExecutorService service = Executors.newFixedThreadPool(10);
private RestTemplate restTemplate = new RestTemplate();

@Override
public String executeSync(ClientKey keys) {

String response = null;
try {

ClientTask ClientTask =

Solution

If you have indeed identified the json deserialization to be a performance problem (by profiling it) then you can

  • Try using a different json serializer (GSon was reported at least in the past to be a fair bit slower than for example Jackson)



  • Don't deserialize and check the string manually.



From "one of my servers" I conclude that you control exactly what responses the server can send and how they are composed. In which case a simple:

if (response.contains("error") || response.contains("warning")) {
    ...
}


might suffice and be robust enough. When handling the error case you could then either still deserialize the message or just use some string splitting or regular expressions in order to extract the data for logging. Or you could simply log the entire json - it's human readable and a log parser could easily extract the relevant data for analysis afterwards.

Code Snippets

if (response.contains("error") || response.contains("warning")) {
    ...
}

Context

StackExchange Code Review Q#40079, answer score: 3

Revisions (0)

No revisions yet.