Ho riscontrato lo stesso problema di this question, utilizzando Spring Boot 1.3.0 e non avendo i miei controller annotati con @RestController
, solo @Path
e @Service
. Come il PO in questa domanda dice,Come gestire il reindirizzamento di Spring Boot su/error?
questo è, per me, tutt'altro che sensibile
anche io non riesco a capire perché avrebbero dovuto farlo reindirizzare/errore. E lo è è molto probabile che mi manca qualcosa, perché posso solo restituire 404 o 200 al client.
Il mio problema è che la sua soluzione non sembra funzionare con 1.3.0, quindi ho il seguente flusso di richieste: diciamo che il mio codice genera uno NullPointerException
. Sarà gestita da uno dei miei ExceptionMapper
s
@Provider
public class GeneralExceptionMapper implements ExceptionMapper<Throwable> {
private static final Logger LOGGER = LoggerFactory.getLogger(GeneralExceptionMapper.class);
@Override
public Response toResponse(Throwable exception) {
LOGGER.error(exception.getLocalizedMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
E il mio codice restituisce una 500, ma invece di inviarlo al client, si tenta di reindirizzare a/errore. Se non ho un'altra risorsa per questo, che sarà mandare indietro il lato una 404.
2015-12-16 18:33:21.268 INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter : 1 * Server has received a request on thread http-nio-8080-exec-1
1 > GET http://localhost:8080/nullpointerexception
1 > accept: */*
1 > host: localhost:8080
1 > user-agent: curl/7.45.0
2015-12-16 18:33:29.492 INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter : 1 * Server responded with a response on thread http-nio-8080-exec-1
1 < 500
2015-12-16 18:33:29.540 INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter : 2 * Server has received a request on thread http-nio-8080-exec-1
2 > GET http://localhost:8080/error
2 > accept: */*
2 > host: localhost:8080
2 > user-agent: curl/7.45.0
2015-12-16 18:33:37.249 INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter : 2 * Server responded with a response on thread http-nio-8080-exec-1
2 < 404
E del cliente (ricciolo):
$ curl -v http://localhost:8080/nullpointerexception
* STATE: INIT => CONNECT handle 0x6000572d0; line 1090 (connection #-5000)
* Added connection 0. The cache now contains 1 members
* Trying ::1...
* STATE: CONNECT => WAITCONNECT handle 0x6000572d0; line 1143 (connection #0)
* Connected to localhost (::1) port 8080 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x6000572d0; line 1240 (connection #0)
* STATE: SENDPROTOCONNECT => DO handle 0x6000572d0; line 1258 (connection #0)
> GET /nullpointerexception HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.45.0
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x6000572d0; line 1337 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x6000572d0; line 1464 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x6000572d0; line 1474 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 404 Not Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Length: 0
< Date: Wed, 16 Dec 2015 17:33:37 GMT
<
* STATE: PERFORM => DONE handle 0x6000572d0; line 1632 (connection #0)
* Curl_done
* Connection #0 to host localhost left intact
Quindi è sempre una 404. A meno che io ce l'ho tale risorsa/errore, allora cosa? cosa dovrei tornare? Tutto quello che ho a questo punto è una richiesta di GET per/errore. E non voglio quelle richieste in più che consumano risorse e inquinano i miei registri.
Cosa mi manca? E se nulla, cosa dovrei fare con la mia gestione delle eccezioni?
funziona come un incanto – mark951131b