相关疑难解决方法(0)

如何将Jersey ExceptionMapper与Google Guice一起使用?

我正在使用Jersey Guice,需要配置自定义 ExceptionMapper

我的模块看起来像这样:

public final class MyJerseyModule extends JerseyServletModule
{
   @Override
   protected void configureServlets()
   {
      ...
      filter("/*").through(GuiceContainer.class);
      ...
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我的ExceptionMapper:

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;

public class MyExceptionMapper implements ExceptionMapper<MyException>
{
   @Override
   public Response toResponse(final MyException exception)
   {
      return Response.status(Status.NOT_FOUND).entity(exception.getMessage()).build();
   }
}
Run Code Online (Sandbox Code Playgroud)

java jersey guice

9
推荐指数
1
解决办法
3653
查看次数

Jersey ExceptionMapper不映射异常

我的Spring Boot + Jersey REST服务无法按预期工作.

UserController抛出了EmailExistsException,但我只收到错误500.一直都是.我的例外情况没有记录.

我怀疑异常处理存在一些配置问题,但不知道在哪里设置它.有什么建议?

@POST
@Path("register")
@Produces(MediaType.APPLICATION_JSON)
public Response register(NewUserPayload newUserPayload) throws EmailExistsException, MessagingException
Run Code Online (Sandbox Code Playgroud)

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);
}
}
Run Code Online (Sandbox Code Playgroud)

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);
} …
Run Code Online (Sandbox Code Playgroud)

rest spring web-services jersey spring-boot

8
推荐指数
1
解决办法
9513
查看次数

标签 统计

jersey ×2

guice ×1

java ×1

rest ×1

spring ×1

spring-boot ×1

web-services ×1