0

我正在尝试检索在 ExceptionMapper 中的请求中传递的 ApplicationRequest 对象,并将其发送回错误响应中。

感谢任何帮助。

@POST
@Path("detail")
@Produces(MediaType.APPLICATION_JSON)
public Response getTokenDetails(@Valid ApplicationRequest appRequest) {
        
    return Response.ok()
                .entity(appSvc.getDetails(appRequest))
                .build();
}

@Provider
public static class ValidationExceptionMapper implements ExceptionMapper<javax.validation.ValidationException> {

    @Override
    @Counted(name = "ValidationExceptionMapperCount")
    public Response toResponse(javax.validation.ValidationException exception) {

        final ConstraintViolationException cve = (ConstraintViolationException) exception;
        final Set<ConstraintViolation<?>> constraintViolations = cve.getConstraintViolations();
        Set<String> messages = new HashSet<>(constraintViolations.size());
        messages.addAll(constraintViolations.stream()
                .map(constraintViolation -> String.format("'%s' : passed value '%s'", constraintViolation.getMessage() ,constraintViolation.getInvalidValue()))
                .collect(Collectors.toList()));

        Response resp = Response.status(Status.BAD_REQUEST)
                .entity(ErrorResponse.builder()
                        .errorCode("400")
                        .errorMessageText("BAD ARGUMENT (One or more arguments are invalid)")
                        .applicationMessageText(messages.toString())
                        .applicationRequest(appRequest.toString())
                        .build())
                .build();

        log.info(resp.getEntity().toString());


        return resp;
    }   
}    
4

0 回答 0