dịch vụ Spring Boot + Jersey REST của tôi không hoạt động như mong đợi.Jersey ExceptionMapper không ánh xạ ngoại lệ
EmailExistsException được ném trong UserController nhưng tôi chỉ nhận được lỗi 500. Tất cả thời gian. Và ngoại lệ của tôi không được ghi lại.
Tôi nghi ngờ có một số vấn đề về cấu hình với xử lý ngoại lệ nhưng không biết phải thiết lập ở đâu. Bất kỳ đề xuất?
@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'
không có thông tin về cấu hình này trong tài liệu [jersey] (https://jersey.java.net/documentation/latest/representations.html#d0e6653). Tuyên bố 'gói' có tránh bạn khai báo các bộ điều khiển không? – herau
Tôi đã tìm thấy nó trong một số đoạn mã trên internet. Tôi đang ở Scala bây giờ nên không thể cho bạn biết thêm thông tin về nó. – Reeebuuk
Cảm ơn! Nó chỉ ra bạn phải hoặc là 'đăng ký (YourExceptionMapper.class)' hoặc đặt nó trong một gói được quét qua 'gói (" YourExceptionMapperPackage ")'. Tôi đã dành vài giờ cố gắng tìm ra điều này trước khi tôi tìm thấy câu trả lời của bạn. –