2015-01-16 66 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>