squarepegsys / error-prone-restemplate

A Sample Spring Boot App that shows how to handle outside services that don't always send 200

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error Services

This is a contrived example to show how you can handle non-200 responses from Spring’s ~RestTemplate~.

The project problem-service is a simple Spring Boot app that doubles any number coming in, but every third time it sends an error back. The project consumer has a service that calls the doubling service in problem-service and gives back the result.

Run ./gradlew bootRun in both project to run them both.

Port and Endpoint information:

ProjectPortEndpointResponse Status
problem-service9090/someService/{value}/double200 or 406
consumer8080/myService/{value}/calculate200

Example of hitting the service in consumer

> curl -v http://localhost:8080//myService/42/calculate
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET //myService/42/calculate HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.56.1
> Accept: */*
>
< HTTP/1.1 200
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Wed, 29 Nov 2017 16:13:06 GMT
<
* Connection #0 to host localhost left intact
{"result":84,"error":null}% 

> curl -v http://localhost:8080//myService/42/calculate
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET //myService/42/calculate HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.56.1
> Accept: */*
>
< HTTP/1.1 200
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Wed, 29 Nov 2017 16:13:08 GMT
<
* Connection #0 to host localhost left intact
{"result":null,"error":{"description":"Leeroy Jenkins!"}}% 

About

A Sample Spring Boot App that shows how to handle outside services that don't always send 200


Languages

Language:Java 100.0%