@Test public void badRequestIfAccountHasValidationError() throws Exception { AccountDto accountDto = new AccountDto(); when(invocationContext.getParameters()).thenReturn(new Object[] {accountDto}); Object result = interceptor.aroundInvoke(invocationContext); assertThat(result).isInstanceOf(Response.class); assertThat(((Response) result).getStatus()).isEqualTo(BAD_REQUEST.getStatusCode()); accountDto.setName("name"); accountDto.setAccountType(AccountType.Normal); result = interceptor.aroundInvoke(invocationContext); assertThat(result).isInstanceOf(Response.class); assertThat(((Response) result).getStatus()).isEqualTo(BAD_REQUEST.getStatusCode()); }
@AroundInvoke public Object aroundInvoke(InvocationContext invocation) throws Exception { ValidatePayload validatePayload = getAnnotationFromClassOrMethod(invocation); Class payloadType = validatePayload.value(); Optional<Object> matchedParam = Arrays.stream(invocation.getParameters()).filter( payloadType::isInstance).findAny(); if (matchedParam.isPresent()) { Object param = matchedParam.get(); Set<ConstraintViolation<Object>> violations = validator.validate(param); if (!violations.isEmpty()) { String message = violations.stream().map( v -> String.format("%s %s", v.getPropertyPath(), v.getMessage())) .reduce("error: ", (a, b) -> a + b + "; "); log.info("invalid payload for {}: {}", invocation.getMethod(), message); return Response.status(Response.Status.BAD_REQUEST).entity( new APIResponse(Response.Status.BAD_REQUEST, message)) .build(); } } return invocation.proceed(); }
ValidatePayloadInterceptor implements Serializable { @AroundInvoke public Object aroundInvoke(InvocationContext invocation) throws Exception { ValidatePayload validatePayload = getAnnotationFromClassOrMethod(invocation); Class payloadType = validatePayload.value(); Optional<Object> matchedParam = Arrays.stream(invocation.getParameters()).filter( payloadType::isInstance).findAny(); if (matchedParam.isPresent()) { Object param = matchedParam.get(); Set<ConstraintViolation<Object>> violations = validator.validate(param); if (!violations.isEmpty()) { String message = violations.stream().map( v -> String.format("%s %s", v.getPropertyPath(), v.getMessage())) .reduce("error: ", (a, b) -> a + b + "; "); log.info("invalid payload for {}: {}", invocation.getMethod(), message); return Response.status(Response.Status.BAD_REQUEST).entity( new APIResponse(Response.Status.BAD_REQUEST, message)) .build(); } } return invocation.proceed(); } }
ValidatePayloadInterceptor implements Serializable { @AroundInvoke public Object aroundInvoke(InvocationContext invocation) throws Exception { ValidatePayload validatePayload = getAnnotationFromClassOrMethod(invocation); Class payloadType = validatePayload.value(); Optional<Object> matchedParam = Arrays.stream(invocation.getParameters()).filter( payloadType::isInstance).findAny(); if (matchedParam.isPresent()) { Object param = matchedParam.get(); Set<ConstraintViolation<Object>> violations = validator.validate(param); if (!violations.isEmpty()) { String message = violations.stream().map( v -> String.format("%s %s", v.getPropertyPath(), v.getMessage())) .reduce("error: ", (a, b) -> a + b + "; "); log.info("invalid payload for {}: {}", invocation.getMethod(), message); return Response.status(Response.Status.BAD_REQUEST).entity( new APIResponse(Response.Status.BAD_REQUEST, message)) .build(); } } return invocation.proceed(); } @Inject  ValidatePayloadInterceptor(Validator validator);  ValidatePayloadInterceptor(); }
ValidatePayloadInterceptor implements Serializable { @AroundInvoke public Object aroundInvoke(InvocationContext invocation) throws Exception { ValidatePayload validatePayload = getAnnotationFromClassOrMethod(invocation); Class payloadType = validatePayload.value(); Optional<Object> matchedParam = Arrays.stream(invocation.getParameters()).filter( payloadType::isInstance).findAny(); if (matchedParam.isPresent()) { Object param = matchedParam.get(); Set<ConstraintViolation<Object>> violations = validator.validate(param); if (!violations.isEmpty()) { String message = violations.stream().map( v -> String.format("%s %s", v.getPropertyPath(), v.getMessage())) .reduce("error: ", (a, b) -> a + b + "; "); log.info("invalid payload for {}: {}", invocation.getMethod(), message); return Response.status(Response.Status.BAD_REQUEST).entity( new APIResponse(Response.Status.BAD_REQUEST, message)) .build(); } } return invocation.proceed(); } @Inject  ValidatePayloadInterceptor(Validator validator);  ValidatePayloadInterceptor(); @AroundInvoke Object aroundInvoke(InvocationContext invocation); }
ValidatePayloadInterceptor implements Serializable { @AroundInvoke public Object aroundInvoke(InvocationContext invocation) throws Exception { ValidatePayload validatePayload = getAnnotationFromClassOrMethod(invocation); Class payloadType = validatePayload.value(); Optional<Object> matchedParam = Arrays.stream(invocation.getParameters()).filter( payloadType::isInstance).findAny(); if (matchedParam.isPresent()) { Object param = matchedParam.get(); Set<ConstraintViolation<Object>> violations = validator.validate(param); if (!violations.isEmpty()) { String message = violations.stream().map( v -> String.format("%s %s", v.getPropertyPath(), v.getMessage())) .reduce("error: ", (a, b) -> a + b + "; "); log.info("invalid payload for {}: {}", invocation.getMethod(), message); return Response.status(Response.Status.BAD_REQUEST).entity( new APIResponse(Response.Status.BAD_REQUEST, message)) .build(); } } return invocation.proceed(); } @Inject  ValidatePayloadInterceptor(Validator validator);  ValidatePayloadInterceptor(); @AroundInvoke Object aroundInvoke(InvocationContext invocation);  }