Retrofit global error handle - java

Well, as we know, if the server's reponse is formatted like this:
{data: T,status: 200,error:""}
it's easy to have your base response like this:
public class HttpResult<T> {
public int satus;
public String error;
public T data;
}
But what if the server return a JSON object when succeeding, and return another
JSON object when failed, for example:
{
"error": "Consumer key missing.",
"status": 401
}
Now, how can I write a base class to encapsulate the response , for both succeed and error result?
Or should I let GSON parse different JSON objects in these two situations?
PS: I use GSON and retrofit + rxjava in this project...
Can anyone give me some advice?
Thanks in advance

In GSON library, you no need to handle it because if your parameter is missing it will automatically return null.
public class HttpResult<T>
{
public int satus;
public String error;
public T data;
}
In above class you will get T as a null.

When you use RxJava with Retrofit and define your request return type as Observable<Result> where Result is the object type you want to deserialize the response to, RxJava automatically emits only responses with HTTP status code between <200,300) which are considered as a success.. Every other response that gets back with a different status code is considered as a failure and will throw HttpException, thus you can do your error handling in onError callback as follows:
create(ApiService.class).someRequest()
.subscribe((result) -> {
// process your successful result
}, (throwable) -> {
if(throwable instanceof HttpException){
switch (((HttpException) throwable).code()) {
case 401: // handle this type of error
default: // do whatever you want when you don't expect this type of error to happen
}
} else {
// error returned is not related to a failed response
// and is probably a result of an exception along the
// way in the stream ( wrong (de)serialization, no network
// connection or whatever you might mess up in the stream )
}
})
However, by the returned syntax you provided, I guess that your server ignores the HTTP standards and sends you the status code within the json body itself. And although this is not a good way to go and you might wanna discuss this with whoever is responsible for your backend, there is of course a way around. Retrofit itself cannot handle this without your help as it doesn't know which field corresponds to the status code. But you can modify the stream so that it behaves like you want. If you define the request with the returned HttpResult as you described, then you can do something like this:
create(ApiService.class).someRequest()
.map((result) -> {
if(result.status >= 200 && result.status < 300) return result.data;
else throw new MyHttpException(result.error, result.status);
})
.subscribe((result) -> {
// process your successful result
}, (throwable) -> {
if(throwable instanceof MyHttpException){
switch (((MyHttpException) throwable).code) {
case 401: // handle this type of error
default: // do whatever you want when you don't expect this type of error to happen
}
} else {
// error returned is not related to a failed response
// and is probably a result of an exception along the
// way in the stream ( wrong (de)serialization, no network
// connection or whatever you might mess up in the stream )
}
})
public class MyHttpException extends RuntimeException {
public String message;
public int code;
public MyHttpException(String message, int code) {
super(message);
this.message = message;
this.code = code;
}
}
You can sugarcoat this a little bit by encapsulating the map operator into a Transformer so you can just apply it through .compose(). Unfortunately, there is no good way how to do this globally until the guys at Retrofit accept the pull request ( or implement it by their own ) for defining a global transformer for all requests.

Related

Java Spring Boot Webflux - Mono response when there is no http body, but just http status

Small question regarding Spring Boot Webflux 2.5.0 and how to deal with a http response without body.
By "without body" I mean:
For instance, a web application I consume the rest API and have no control returns:
HTTP status code 200
HTTP body {"foo": "bar"}
With Spring Webflux, we can easily write something like:
public Mono<FooBar> sendRequest(SomeRequest someRequest) {
return webClient.mutate()
.baseUrl("https://third-party-rest-api.com:443")
.build()
.post()
.uri(/someroute)
.body(BodyInserters.fromValue(someRequest))
.retrieve().bodyToMono(FooBar.class);
}
public class FooBar {
private String foo;
//getter setters
}
In order to get the POJO corresponding to the http body.
Now, another third party API I am consuming only return HTTP 200 as status response.
I would like to emphasize, there is no HTTP body. It is not the empty JSON {}.
Hence, I am a bit lost, and do not know what to put here. Especially with the goal of avoiding the mono empty.
public Mono<WhatToPutHerePlease> sendRequest(SomeRequest someRequest) {
return webClient.mutate()
.baseUrl("https://third-party-rest-api.com:443")
.build()
.post()
.uri(/someroute-with-no-http-body-response)
.body(BodyInserters.fromValue(someRequest))
.retrieve()
.bodyToMono(WhatToPutHerePlease.class);
}
Any help please?
Thank you
Hence, I am a bit lost, and do not know what to put here.
The response is empty, so there's nothing for your webclient to parse and return a value. The resulting Mono is thus always going to be empty, whatever generic type you use.
We have a special type that essentially says "this will always be empty" - Void (note the capital V.) So if you want to return an empty Mono, keeping the rest of the code the same, that's the type you should use.
Alternatively, if you don't want to return an empty publisher, then you might consider using .retrieve().toBodiLessEntity() instead of .retrieve().bodyToMono() - this will return a Mono<ResponseEntity<Void>>. The resulting body will obviously still be empty, but the response entity returned will enable you to extract information such as the response code & header information, should that be useful.
toBodylessEntity() seems to suit your needs:
It returns a Mono<ResponseBody<Void>>.
With a (void rest) controller like:
#RestController
#SpringBootApplication
public class Demo {
public static void main(String[] args) {
SpringApplication.run(Demo.class, args);
// ...
}
#GetMapping("/")
public void empty() {
}
}
and a:
public class ReactiveClient {
Mono<ResponseEntity<Void>> mono = WebClient.create("http://localhost:8080")
.get()
.retrieve()
.toBodilessEntity();
// blocking/synchronous
public ResponseEntity<Void> get() {
return mono.block();
}
}
We can:
ReactiveClient reactiveClient = new ReactiveClient();
System.out.println(reactiveClient.get()); // or something else

Spring ResponseBodyEmitter doesn't set Content-Type header in a response

I'm writing a kind of a wrapper around my request handlers to make them stream HTTP response. What I've got now is
Handler response wrapper:
#Component
public class ResponseBodyEmitterProcessor {
public ResponseBodyEmitter process(Supplier<?> supplier) {
ResponseBodyEmitter emitter = new ResponseBodyEmitter();
Executors.newSingleThreadExecutor()
.execute(() -> {
CompletableFuture<?> future = CompletableFuture.supplyAsync(supplier)
.thenAccept(result -> {
try {
emitter.send(result, MediaType.APPLICATION_JSON_UTF8);
emitter.complete();
} catch (IOException e) {
emitter.completeWithError(e);
}
});
while (!future.isDone()) {
try {
emitter.send("", MediaType.APPLICATION_JSON_UTF8);
Thread.sleep(500);
} catch (IOException | InterruptedException e) {
emitter.completeWithError(e);
}
}
});
return emitter;
}
}
Controller:
#RestController
#RequestMapping("/something")
public class MyController extends AbstractController {
#GetMapping(value = "/anything")
public ResponseEntity<ResponseBodyEmitter> getAnything() {
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(process(() -> {
//long operation
}));
}
What I'm doing is just send empty string every half a second to keep a request alive. It's required for some tool to not shut it down by timeout. The problem here that I don't see any Content-Type header in a response. There's nothing at all, despite I return ResponseEntity from my controller method as it's said in this thread:
https://github.com/spring-projects/spring-framework/issues/18518
Looks like only TEXT_HTML media type is acceptable for streaming. Isn't there a way to stream json at all? I even manually mapped my dtos to json string using objectMapper, but still no luck. Tried with APPLICATION_JSON and APPLICATION_STREAM_JSON - doesn't work. Tried in different browsers - the same result.
I also manually set Content-Type header for ResponseEntity in the controller. Now there's the header in a response, but I'm not sure, if my data is actually streamed. In Chrome I can only see the result of my operation, not intermediate chars that I'm sending (changed them to "a" for test).
I checked the timing of request processing for two options:
Without emitter (just usual controller handler)
With emitter
As I understand Waiting status means: "Waiting for the first byte to appear". Seems like with emitter the first byte appears much earlier - this looks like what I need. Can I consider it as a proof that my solution works?
Maybe there's another way to do it in Spring? What I need is just to notify the browser that a request is still being processed by sending some useless data to it until the actual operation is done - then return the result.
Any help would be really appreciated.
Looking at the source of ResponseBodyEmitter#send it seems that the specified MediaType should have been set in the AbstractHttpMessageConverter#addDefaultHeaders method but only when no other contentType header is already present.
protected void addDefaultHeaders(HttpHeaders headers, T t, MediaType contentType) throws IOException{
if (headers.getContentType() == null) {
MediaType contentTypeToUse = contentType;
// ...
if (contentTypeToUse != null) {
headers.setContentType(contentTypeToUse);
}
}
// ...
}
I would suggest to set a break point there and have a look why the header is not applied. Maybe the #RestController sets a default header.
As a workaround you could try the set the contentType header via an annotation in the MVC controller.
E.g.
#RequestMapping(value = "/something", produces = MediaType.APPLICATION_JSON_UTF8)

How to throw exception based on feign.Response?

I have a Feign client with a method returning the feign.Response class. When another service throws an exception, feign puts an exception message on response body and puts status, but my service does not throw an exception. Can I throw an exception based on what I received in response like when I use ResponseEntity.
Feign client
#FeignClient(name = "ms-filestorage")
#RequestMapping(value = "/files", produces = "application/json")
public interface FileStorageApi {
#GetMapping(value = "/{id}")
Response getFileById(#PathVariable String id);
}
Usage of client
#Override
public Response getFileFromStorage(String fileId) {
Response fileStorageResponse = fileStorageApi.getFileById(fileId);
// NOW I USE THIS WAY FOR CHECKING RESPONSE BUT IT DOESN'T LOOK GOOD
//if (fileStorageResponse.status() != HttpStatus.OK.value()) {
// throw new OsagoServiceException();
//}
return fileStorageResponse;
}
Usually, if a Feign client call receives an error response from the API it is calling, it throws a FeignException.
This can be caught in a try / catch block (or a Feign ErrorDecoder if you want to be more sophisticated, but that's another post).
However, this is not the case if you map the error response into a Feign.Response return type - see this Github issue.
Instead of returning Feign.Response from getFileFromStorage(), you should create a custom Java object to hold the response, and you will then have access to the FeignException which you can handle as you wish.
Note that if you don't need access to the data that is returned from the API you are calling, changing the return type to void will also resolve this issue.

How to get message body from RoutingContext?

I created a vert.x HTTP server and I want to receive messages from clients in json format.
This is my code:
public class HttpServerVerticle extends AbstractVerticle {
#Override
public void start(Future<Void> future) {
Router router = Router.router(vertx)
.exceptionHandler(ex -> log.error("Error: " + ex));
router.route().handler(BodyHandler.create());
router.route("/getData").handler(this::getRequestHandler);
HttpServerOptions serverOptions = new HttpServerOptions();
vertx.createHttpServer(serverOptions)
.requestHandler(router::accept)
.listen(9539, result -> {
if (result.succeeded()) {
future.complete();
} else {
future.fail(result.cause());
}
});
}
private void getRequestHandler(RoutingContext request) {
JsonObject data = request.getBodyAsJson(); // In this place I had exception
...
request.response().end("{\"reply\":\"ok\"}");
}
}
When my handler was fire and I get some message from the client I get this exception:
io.vertx.core.json.DecodeException: Failed to decode:Unexpected end-of-input: expected close marker for Object (start marker at [Source: (String)"{"ClientRequest":["ok"],"Type":"Test"[truncated 678 chars]; line: 1, column: 1])
How can I avoid this exception and all time get the full message?
You should specify the HTTP method
router.route("/getData").handler(this::getRequestHandler);
is expecting an HTTP GET, but what you need is an HTTP POST:
router.post("/getData")
.consumes("application/json")
.handler(this::getRequestHandler);
will accept json in the body. The consumes is optional and enforces the client to only send json.
I would also suggest to name the route "/data" instead of "/getData" to conform with the concept of RESTful APIs.
Its possible the client is not sending a valid json.
This is a valid exception to handle in that case.
For logging the request, you can simply use request.getBodyAsString() before doing getBodyAsJson()

Error message in REST webservices

I am calling REST webservices from JSP using AJAX . Can you tell me the best way to send custom error message from REST webservice to JSP ?
Consider using HTTP response codes with (possibly) json response bodies to supply any required information so the client application can react accordingly.
Consider using the WebapplicationException. You can give it the Errorcode (also custom ones) and a body for the response. You could use the JSON Format if you have a complex structure to display your errors but i would suggest just using the an errormessage (for example in case of a bad request, what part of the request was bad).
If you are using JAX-RS REST webservice, you can configure Spring #Controller. Your method should produce application/json and return Response object, like in this example:
#GET
#Path("/get/{id}")
#Produces(MediaType.APPLICATION_JSON)
public Response getUserById(#PathParam("id") String userId) {
// Here your logic
Foo foo = new Foo();
foo.setMsg("Bad Request");
foo.setData("User " + userId + " not found")
return Response.status(400).entity(foo).build();
}
And from AJAX, you can catch error message
// Get user details
$.getJSON(encodeURI("./rest/user/get/" + userId), function(data) {
// Some logic on success
// Fail
}).fail( function(jqxhr) {
console.log(jqxhr.responseJSON.msg);
});
There are a couple of ways.
1. You can look at the response status you receive from the web service. The statuses starting with 2** are a success response (Eg: 200, 201), the ones starting with 4** or 5** are errors.
But the optimal way to handle and track exceptions is to use ExceptionMapper. You can write your own class that implements ExceptionMapper like below:
#Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {
#Override
public Response toResponse(Throwable arg0) {
return Response.status(Status.INTERNAL_SERVER_ERROR)
.entity("Custom Exception: Error retrieving data")
.build();
}
}
You can write your own custom exceptions like below or can throw blanket exception like below. The above approach is the preferred one though.
#Provider
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {
#Override
public Response toResponse(Throwable arg0) {
return Response.status(Status.INTERNAL_SERVER_ERROR)
.entity("Custom Exception: Error retrieving data")
.build();
}
}

Categories

Resources