-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle and output errors occurred when calling GreetMe server
- Loading branch information
Showing
5 changed files
with
133 additions
and
36 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/poc/openshift/greetme/web/client/ErrorObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package poc.openshift.greetme.web.client; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategy; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import org.springframework.web.client.RestClientException; | ||
|
||
import java.util.UUID; | ||
|
||
@NoArgsConstructor | ||
@Getter | ||
@Setter | ||
@ToString | ||
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) | ||
public class ErrorObject<T> { | ||
|
||
private String errorMessage; | ||
private T errorDetails; // T is of type String or List<String> | ||
private String errorId; | ||
|
||
public ErrorObject(RestClientException e) { | ||
errorMessage = "Could not communicate with GreetMe server"; | ||
errorDetails = (T) e.getMessage(); | ||
errorId = UUID.randomUUID().toString(); | ||
} | ||
} |
61 changes: 47 additions & 14 deletions
61
src/main/java/poc/openshift/greetme/web/client/GreetMeServerClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,77 @@ | ||
package poc.openshift.greetme.web.client; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.HttpStatusCodeException; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
import poc.openshift.greetme.web.controller.Greeting; | ||
import poc.openshift.greetme.web.controller.Person; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.function.Supplier; | ||
|
||
@Component | ||
@Slf4j | ||
public class GreetMeServerClient { | ||
|
||
private RestTemplate serverTemplate; | ||
private String greetingsResourceUrl; | ||
private ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
public GreetMeServerClient(@Value("${greetme.server.baseurl}") String greetMeServerBaseUrl, | ||
@Value("${greetme.server.greetings.resource.url}") String greetingsResourceUrl) { | ||
@Value("${greetme.server.greetings.resource.url}") String greetingsResourceUrl, | ||
ObjectMapper objectMapper) { | ||
|
||
this.serverTemplate = new RestTemplateBuilder().rootUri(greetMeServerBaseUrl).build(); | ||
this.greetingsResourceUrl = greetingsResourceUrl; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
public Object postPersonToGreet(Person person) { | ||
Supplier<Object> postPersonToGreetCall = () -> serverTemplate.postForObject(greetingsResourceUrl, person, Greeting.class); | ||
return callGreetMeServerWithExceptionHandling(postPersonToGreetCall, "greeting for person: " + person); | ||
} | ||
|
||
public Object getGreetings() { | ||
Supplier<Object> getGreetingsCall = () -> serverTemplate.getForObject(greetingsResourceUrl, Collection.class); | ||
return callGreetMeServerWithExceptionHandling(getGreetingsCall, "all greetings"); | ||
} | ||
|
||
public Greeting postPersonToGreet(Person person) { | ||
log.info("Requesting greeting for {}", person); | ||
Greeting greeting = serverTemplate.postForObject(greetingsResourceUrl, person, Greeting.class); | ||
log.info("Received {}", greeting); | ||
return greeting; | ||
private Object callGreetMeServerWithExceptionHandling(Supplier<?> greetMeServerCall, String callDescription) { | ||
log.info("Requesting {}", callDescription); | ||
Object result; | ||
try { | ||
result = greetMeServerCall.get(); | ||
log.info("Received {}: {}", callDescription, result); | ||
} | ||
catch (HttpStatusCodeException e) { | ||
result = deserializeJsonErrorContent(e); | ||
log.error("Received error for requesting {}: {}", callDescription, result); | ||
} | ||
catch (RestClientException e) { | ||
result = new ErrorObject<String>(e); | ||
log.error("Received error for requesting {}: {}", callDescription, result); | ||
} | ||
return result; | ||
} | ||
|
||
public Collection<Greeting> getGreetings() { | ||
log.info("Requesting all greetings"); | ||
Collection<Greeting> greetings = serverTemplate.exchange(greetingsResourceUrl, HttpMethod.GET, null, new ParameterizedTypeReference<Collection<Greeting>>() { | ||
}).getBody(); | ||
log.info("Received all greetings: {}", greetings); | ||
return greetings; | ||
private ErrorObject<?> deserializeJsonErrorContent(HttpStatusCodeException hsce) { | ||
String jsonErrorContent = hsce.getResponseBodyAsString(); | ||
try { | ||
return objectMapper.readValue(jsonErrorContent, ErrorObject.class); | ||
} | ||
catch (IOException ioe) { | ||
String message = "Could not deserialize JSON error content to ErrorObject: " + jsonErrorContent; | ||
RuntimeException re = new RuntimeException(message, ioe); | ||
log.error(message, re); | ||
throw re; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters