Zwracanie błedu przy wyjątku w kontrolerze

0

Cześć,

mam w kontrolerze metodę która zwraca dokument Word:

    @RequestMapping(method = RequestMethod.GET, produces = "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
    @ResponseBody
    public ResponseEntity<Resource> getFile() {
        Resource file = storageService.loadTemplateResource();
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
                .body(file);
    }

Jednak kiedy storageService.loadTemplateResource() rzuca wyjątek, metoda zwraca puste body.

W jaki sposób mogę zwrócić springowy komunikat o błędzię (w formacie text/plain i application/json)?

Zauważyłem że po usunięciu produces = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" z @RequestMapping komunikat jest zwracany poprawnie.

0

Chcesz po prostu zwrócić 500 internal server error ??

0
rubaszny_karp napisał(a):

Chcesz po prostu zwrócić 500 internal server error ??

Nie, chodzi o springowy response, np.:

{
  "timestamp" : "2018-02-06T15:27:18.881+0000",
  "status" : 404,
  "error" : "Not Found",
  "message" : "No message available",
  "path" : "/api/"
}
0
rubaszny_karp napisał(a):

https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

Ok, teraz po dodaniu @ResponseStatus do wyjątku:

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class TemplateStorageFileNotFoundException extends TemplateStorageException {
    TemplateStorageFileNotFoundException(String s) {
        super(s);
    }

    TemplateStorageFileNotFoundException(String s, Throwable cause) {
        super(s, cause);
    }
}

Dostaje puste body i HttpStatus 406 w przypadku Accept: application/json, w przypadku Accept: text/plain (przez browser) błąd się zgadza:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Feb 07 18:39:54 CET 2018
There was an unexpected error (type=Not Found, status=404).
Failed to read stored file

Jednak po usunięciu produces = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" dostaje dobry HttpStatus i body z jsonem:

Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 07 Feb 2018 17:41:32 GMT

{
  "timestamp": 1518025292278,
  "status": 404,
  "error": "Not Found",
  "exception": "pl.service.TemplateStorageFileNotFoundException",
  "message": "Failed to read stored file",
  "path": "/template/"
}

Metoda loadTemplateResource()


@Override
    public Resource loadTemplateResource() {
        try {
            Resource resource = new UrlResource(loadTemplate().toUri());
            if (resource.exists() && resource.isReadable()) {
                return resource;
            } else {
                throw new TemplateStorageFileNotFoundException("Failed to read stored file");
            }
        } catch (MalformedURLException e) {
            throw new TemplateStorageFileNotFoundException("Failed to read stored file", e);
        }
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1