Created
March 30, 2020 12:25
-
-
Save manish2aug/2f5d1e317e2b5bffbec67c7ffbf403ba to your computer and use it in GitHub Desktop.
CompletionStageIssue
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
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider; | |
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
import java.util.List; | |
import java.util.concurrent.CompletionStage; | |
@RegisterRestClient(baseUri = "https://jsonplaceholder.typicode.com") | |
@RegisterProvider(ClientLoggingFilter.class) | |
@RegisterProvider(TempExceptionResponseMapper.class) | |
public interface TempClient { | |
@GET | |
@Path("/todos") | |
@Produces(Constants.APPLICATION_JSON) | |
CompletionStage<List<Todo>> getTodos() throws TempException; | |
} |
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
package momentum.retail.via_bff.client.responseexceptionmapper; | |
import momentum.retail.via_bff.domain.exception.TempException; | |
import org.apache.commons.lang3.StringUtils; | |
import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.ext.Provider; | |
import java.util.logging.Logger; | |
@Provider | |
public class TempExceptionResponseMapper implements ResponseExceptionMapper<TempException> { | |
private static final Logger log = Logger.getLogger("TempExceptionResponseMapper"); | |
@Override | |
public TempException toThrowable(Response response) { | |
int status = response.getStatus(); | |
String statusMsg = String.join("\n", | |
"\n---------------------------\nClient call failed: \n---------------------------", | |
String.format("Status: %s", status)); | |
TempException TempException; | |
try { | |
String message = response.readEntity(String.class); | |
TempException = new TempException(status, message); | |
statusMsg = String.join("\n", statusMsg, | |
String.format("Message: %s", | |
StringUtils.isNotBlank(message) ? message : "No message available"), | |
""); | |
log.finest(statusMsg); | |
} catch (Exception e) { | |
String message = e.getMessage(); | |
statusMsg = String.join("\n", statusMsg, | |
String.format("Client call failed and response entity couldn't be read: %s", | |
StringUtils.isNotBlank(message) ? message : "No message available"), | |
""); | |
TempException = new TempException(status, message); | |
TempException.addSuppressed(e); | |
log.finest(statusMsg); | |
} | |
return TempException; | |
} | |
} |
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
import java.util.List; | |
public interface TempRepository { | |
List<Todo> getTodos() throws TempException; | |
} |
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
import org.eclipse.microprofile.rest.client.inject.RestClient; | |
import javax.enterprise.context.ApplicationScoped; | |
import javax.inject.Inject; | |
import java.util.List; | |
@ApplicationScoped | |
public class TempRepositoryImpl implements TempRepository { | |
@Inject | |
@RestClient | |
private TempClient tempClient; | |
@Override | |
public List<Todo> getTodos() throws TempException { | |
List<Todo> join = tempClient.getTodos().exceptionally(throwable -> null).toCompletableFuture().join(); | |
System.out.println(join.get(0).getTitle()); | |
return join; | |
} | |
} |
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
import javax.enterprise.context.ApplicationScoped; | |
import javax.inject.Inject; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import java.util.logging.Logger; | |
@Produces(MediaType.APPLICATION_JSON) | |
@Path("/temp") | |
@ApplicationScoped | |
public class TempResource { | |
@Inject | |
TempRepository tempRepository; | |
@Inject | |
Logger log; | |
@GET | |
public Response getTodo() throws TempException { | |
return Response.ok().entity(tempRepository.getTodos()).build(); | |
} | |
} |
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
public class Todo { | |
private int userId; | |
private int id; | |
private String title; | |
private boolean completed; | |
public int getUserId() { | |
return userId; | |
} | |
public void setUserId(int userId) { | |
this.userId = userId; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public boolean isCompleted() { | |
return completed; | |
} | |
public void setCompleted(boolean completed) { | |
this.completed = completed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment