il mio servizio Spring Boot + Jersey REST non funziona come previsto.Jersey ExceptionMapper non associa eccezioni
EmailExistsException viene generata in UserController ma ricevo solo l'errore 500. Sempre. E le mie eccezioni non sono registrate.
Sospetto che ci sia qualche problema di configurazione con la gestione delle eccezioni ma non so dove installarlo. Eventuali suggerimenti?
@POST
@Path("register")
@Produces(MediaType.APPLICATION_JSON)
public Response register(NewUserPayload newUserPayload) throws EmailExistsException, MessagingException
EmailExistsExceptionMapper
@Provider
public class EmailExistsExceptionMapper extends AbstractExceptionMapper implements
ExceptionMapper<EmailExistsException>
{
@Override
public Response toResponse(EmailExistsException e)
{
ResponseEntity re = new ResponseEntity(org.springframework.http.HttpStatus.BAD_REQUEST);
return this.errorResponse(HttpStatus.BAD_REQUEST_400, re, e);
}
}
AbstractExceptionMapper
@Slf4j
public abstract class AbstractExceptionMapper
{
protected Response errorResponse(int status, ResponseEntity responseEntity, Throwable t)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
log.error(sw.toString()); // logging stack trace.
return customizeResponse(status, responseEntity);
}
private Response customizeResponse(int status, ResponseEntity responseEntity)
{
return Response.status(status).entity(responseEntity).build();
}
}
build.gradle
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: 'spring-boot-starter-tomcat'
}
compile "org.springframework.boot:spring-boot-starter-jetty"
compile "org.springframework.boot:spring-boot-starter-security"
compile "org.springframework.boot:spring-boot-starter-aop"
compile "org.springframework.boot:spring-boot-starter-data-jpa"
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.boot:spring-boot-starter-jersey"
compile 'org.springframework.boot:spring-boot-starter-mail'
nessuna informazione su questa configurazione nella [documentazione della maglia] (https://jersey.java.net/documentation/latest/representations.html#d0e6653). La dichiarazione 'package' ti eviterà di dichiarare i controller? – herau
L'ho trovato in qualche frammento di codice su internet. Sono in Scala ora, quindi non potrei dirti più informazioni a riguardo. – Reeebuuk
Grazie! Si scopre che devi esplicitamente "registrare (YourExceptionMapper.class)" o metterlo in un pacchetto scansionato tramite 'packages (" YourExceptionMapperPackage ")'. Ho passato un paio d'ore a cercare di capirlo prima di trovare la tua risposta. –