有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ConflictExceptionMapper未捕获抛出的java ConflictException

我从新泽西到新泽西,我想在我的服务中加入一个冲突例外(定制), 抓住它,做出回应

下面是我的代码

public class ConflictException extends ClientErrorException {
    public ConflictException() {
        super(Response.Status.CONFLICT); // 409
    }
}

然后我想使用ExceptionMapper发送正确的响应

@Provider
public class ConflictExceptionMapper implements ExceptionMapper<ConflictException> {
    @Override
    public Response toResponse(ConflictException exception) {
        return Response.status(Response.Status.CONFLICT).entity(new ResponseMessage( Collections.emptyList(), OperationResultStatus.Conflict,
                exception.getMessage())).build();
    }
}

不知怎的,这个映射器没有被触发

我做错了什么

更新!!!包括我的主修课

final ResourceConfig rc = new ResourceConfig().packages(packages)
                // Now you can expect validation errors to be sent to the client.
                .property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true)
                // @ValidateOnExecution annotations on subclasses won't cause errors.
                .property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true)
                // Further configuration of ResourceConfig.
                //.property(ServerProperties.PROCESSING_RESPONSE_ERRORS_ENABLED, true)
                .register(new ClassBinder())
                .register(json)
                .register(ConstraintViolationExceptionMapper.class)
                .register(ConflictExceptionMapper.class);

共 (0) 个答案