Jersey ExceptionMapper 不映射异常
Jersey ExceptionMapper doesn't map exceptions
我的 Spring Boot + Jersey REST 服务没有按预期工作。
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'
您是否将自定义 ExceptionMapper 配置为 jax-rs 提供程序,并且您确定您的异常被包装到 EmailExistsException 中吗?你可能要看看这个 post.
JAX-RS using exception mappers
解决我问题的答案:
我已经打包了("package.name");这是我的根包名和异常映射器工作得很好。
@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig
{
public JerseyConfig()
{
packages("package.name");
register(UserController.class);
register(TimezoneController.class);
}
}
我遇到了同样的问题
我刚刚在 <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>
如果您使用 ExceptionMapper,则必须注册您的异常映射器:
@Component
@Provider
public class ApiApplicationExceptionMapper implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception exception) {
...
}
}
在 Jersey 配置 class 中,使用 @Path
注释扫描 Jersey 端点 class 并使用 @Provider
注释扫描自定义异常映射器以注册:
@Configuration
public class JerseyConfig extends ResourceConfig {
@Autowired
ApplicationContext applicationContext;
@PostConstruct
public void init() {
this.registerEndpoints();
this.registerExceptionMappers();
}
private void registerEndpoints() {
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Path.class);
for (Object apiClass : beans.values()) {
logger.info("Jersey register: " + apiClass.getClass().getName());
register(apiClass);
}
}
private void registerExceptionMappers() {
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Provider.class);
for (Object exceptionMapper : beans.values()) {
logger.info("Jersey exception mapper register: " + exceptionMapper.getClass().getName());
register(exceptionMapper);
}
}
我的 Spring Boot + Jersey REST 服务没有按预期工作。
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'
您是否将自定义 ExceptionMapper 配置为 jax-rs 提供程序,并且您确定您的异常被包装到 EmailExistsException 中吗?你可能要看看这个 post.
JAX-RS using exception mappers
解决我问题的答案:
我已经打包了("package.name");这是我的根包名和异常映射器工作得很好。
@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig
{
public JerseyConfig()
{
packages("package.name");
register(UserController.class);
register(TimezoneController.class);
}
}
我遇到了同样的问题
我刚刚在 <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>
如果您使用 ExceptionMapper,则必须注册您的异常映射器:
@Component
@Provider
public class ApiApplicationExceptionMapper implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception exception) {
...
}
}
在 Jersey 配置 class 中,使用 @Path
注释扫描 Jersey 端点 class 并使用 @Provider
注释扫描自定义异常映射器以注册:
@Configuration
public class JerseyConfig extends ResourceConfig {
@Autowired
ApplicationContext applicationContext;
@PostConstruct
public void init() {
this.registerEndpoints();
this.registerExceptionMappers();
}
private void registerEndpoints() {
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Path.class);
for (Object apiClass : beans.values()) {
logger.info("Jersey register: " + apiClass.getClass().getName());
register(apiClass);
}
}
private void registerExceptionMappers() {
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Provider.class);
for (Object exceptionMapper : beans.values()) {
logger.info("Jersey exception mapper register: " + exceptionMapper.getClass().getName());
register(exceptionMapper);
}
}