2015-01-16 65 views
6

我的Spring Boot + Jersey REST服務無法按預期工作。Jersey ExceptionMapper不映射異常

EmailExistsException在UserController中引發,但我只收到錯誤500.所有的時間。而我的例外情況沒有記錄。

我懷疑有一些配置問題與異常處理,但不知道在哪裏設置它。有什麼建議麼?

@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' 

回答

15

回答這個問題解決了我的問題:

我把包( 「package.name」);這是我的根包名稱和異常映射工作的魅力。

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

[球衣文檔]中沒有關於此配置的信息(https://jersey.java.net/documentation/latest/representations.html#d0e6653)。 'package'聲明是否會避免你聲明控制器? – herau

+0

我在互聯網上的一些代碼片段中找到它。我現在在斯卡拉,所以不能告訴你更多關於它的信息。 – Reeebuuk

+3

謝謝!事實證明,你必須顯式地註冊(YourExceptionMapper.class),或者通過packages(「YourExceptionMapperPackage」)把它放在一個掃描的包中。在找到答案之前,我花了幾個小時試圖弄清楚這一點。 –

1

你有沒有配置的自定義ExceptionMapper作爲一個JAX-RS提供者,並且是英語新你的異常是否被包裝到EmailExistsException中?你可能不得不看這個帖子。

JAX-RS using exception mappers

+0

它有@Provider註釋和是的,我敢肯定,它被包裝和拋出。另一件事是,當我試圖在EmailExistsExceptionMapper中放置斷點時,它在調試模式下不在IDEA中。它似乎不知何故不被使用?有些配置可能丟失,但在哪裏? – Reeebuuk

0

我有同樣的問題

我只是在<init-param>

<param-value>標籤提到的web.xml文件中的根包然後它開始像魅力的工作

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