Spring, Angular JS and Exception Handling in Service Layer

Asked

Viewed 734 times

1

I have an app that uses version 6.2.0 with version 3.2.14, , 8 and at front-end we use . The requisitions are made via .

The application is all ready to receive objects as return of the requests, because the requests via are all done via Ajax. In certain functionality of the application we have a "common" request using window.location = url, because we need to return a streaming that is nothing more than an array of bytes to generate a PDF file.

With this, when there is some error in the back-end the answer goes to a blank screen with an object printed in it.

I’ve used error handling with overall or by exception, either using treatment by annotations with Exceptionhandler or with Controlleradvice to generalize, but always treating classes in the control layer with annotation Controller returning an object Modelandview, but never classes in the service layer annotated with Service.

My doubt would be as when capturing the exception to make the redirect to an error screen in my common Get request cases with a parameterized message in this scenario that I described.

1 answer

2

Get the binary content of the file if successful. For this, use the service $http together with Blob:

$http({
    url: 'site/endPointQueRetornaConteudoBinario',
    method: "POST",
    data: json, //caso você precise mandar algum conteúdo no POST body
    headers: {
       'Content-type': 'application/json'
    },
    responseType: 'arraybuffer'
}).success(function (data, status, headers, config) {
    // O resultado é válido:
    var blob = new Blob([data], {type: "application/pdf"});
    var objectUrl = URL.createObjectURL(blob);
    window.open(objectUrl);
}).error(function (data, status, headers, config) {
    //Falhou: Faça seu handler aqui.
});

Source.

  • You could explain a little more what makes the createObjectURL(blob method)?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.