2015-01-16 9 views
6

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' 

risposta

15

risposta che ha risolto i miei problemi:

ho messo pacchetti ("package.name"); quale è il nome del pacchetto root e i mapping delle eccezioni funzionano come un incantesimo.

@Component 
@ApplicationPath("/api") 
public class JerseyConfig extends ResourceConfig 
{ 
public JerseyConfig() 
{ 
    packages("package.name"); 
    register(UserController.class); 
    register(TimezoneController.class); 
} 
} 
+0

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

+0

L'ho trovato in qualche frammento di codice su internet. Sono in Scala ora, quindi non potrei dirti più informazioni a riguardo. – Reeebuuk

+3

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. –

1

Avete configurato personalizzato ExceptionMapper come fornitore di JAX-RS, e Sei s Assicurati che la tua eccezione venga coinvolta in EmailExistsException? Potrebbe essere necessario guardare questo post.

JAX-RS using exception mappers

+0

E ' ha un'annotazione @Provider e sì, sono sicuro che viene incartato e gettato. Un'altra cosa è che quando provo a inserire breakpoint in EmailExistsExceptionMapper non è attivo in IDEA in modalità di debug. Sembra che in qualche modo non sia usato? Manca qualche configurazione, ma dove? – Reeebuuk

0

Ho avuto lo stesso problema

ho appena menzionato il package principale sul file web.xml in <param-value> tag di <init-param>

poi ha cominciato a lavorare come fascino

<servlet> 
    <servlet-name>Jersey Web Application</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>com.two95.restful</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
Problemi correlati