11 Ağustos 2020 Salı

SpringAOP RequestBodyAdvice Arayüzü

Örnek
Şöyle yaparız
@ControllerAdvice
public class CustomRequestBodyAdvice implements RequestBodyAdvice {

  @Override
  public boolean supports(MethodParameter methodParameter, Type targetType,
 Class<? extends HttpMessageConverter<?>> converterType) {
    
    return methodParameter.getContainingClass() == QuestionController.class &&
 targetType.getTypeName() == Question.class.getTypeName();
  }

  @Override
  public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage,
    MethodParameter parameter, Type targetType,
    Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
    System.out.println("In beforeBodyRead() method of " + getClass().getSimpleName());
    return inputMessage;
  }

  @Override
  public Object afterBodyRead(Object body, HttpInputMessage inputMessage,
 MethodParameter parameter, Type targetType,
    Class<? extends HttpMessageConverter<?>> converterType) {
    
    if (body instanceof Question) {
      Question question = (Question) body;
      question.setDate(new Date());
      return question;
    }

    return body;
  }

  @Override
  public Object handleEmptyBody(Object body, HttpInputMessage inputMessage,
 MethodParameter parameter, Type targetType,
    Class<? extends HttpMessageConverter<?>> converterType) {
    
    return body;
  }
}

Hiç yorum yok:

Yorum Gönder