spring-mvc-rest-return-standard-response使用@ControllerAdvice无法正常工作

时间:2017-01-27 12:28:00

标签: spring

我想使用@ControllerAdvice

创建标准响应

我已经编写了一个自定义注释来设置状态代码和消息。

我在

中遇到了类强制转换异常
    @Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    JsonFilter annotation = returnType.getMethodAnnotation(JsonFilter.class);
    String statusCode = annotation.status();
    String message = annotation.message();
    ResponseHeader<Object> responseHeader = new ResponseHeader(statusCode,message,body);
    System.out.println(responseHeader);
    return responseHeader;
}

当我使用JsonFilter annotation = returnType.getMethodAnnotation(JsonFilter.class);

这是我创建的课程 -

@RestControllerAdvice
public class RestResponseHandler implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
    return (AnnotationUtils.findAnnotation(returnType.getContainingClass(), ResponseBody.class) != null ||
            returnType.getMethodAnnotation(ResponseBody.class) != null);
}

@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    JsonFilter annotation = returnType.getMethodAnnotation(JsonFilter.class);
    String statusCode = annotation.status();
    String message = annotation.message();
    ResponseHeader<Object> responseHeader = new ResponseHeader(statusCode,message,body);
    System.out.println(responseHeader);
    return responseHeader;
}


public class CommentController {


    @RequestMapping(value = "/repos", method = RequestMethod.GET)
    @JsonFilter(status="0",message="done")
    public @ResponseBody String repos() throws IOException {
        return null;
    }
}


@Target(ElementType.METHOD)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonFilter {
    String status() default "0";
    String message() ;
}



public class ResponseHeader<Object> {

    private String code;
    private String message;
    private Object body;
    public ResponseHeader(String code, String message,Object body) {
        super();
        this.code = code;
        this.message = message;
        this.body = body;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
    public Object getBody() {
        return body;
    }

1 个答案:

答案 0 :(得分:0)

需要覆盖支持方法。

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        List<Annotation> annotations = Arrays.asList(returnType.getMethodAnnotations());
        return annotations.stream().anyMatch(annotation -> annotation.annotationType().equals(JsonFilter.class));
    }