2017-01-27 11 views
0

を使用して動作していない私は、ステータスコードとメッセージを設定するためのカスタム注釈を書かれている@ControllerAdvice春-MVC-残りリターン標準応答@ControllerAdvice

を使用して、標準の応答を作成します。

@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; 
    } 

答えて

0

Supportsメソッドをオーバーライドする必要がある -

私はここでJsonFilter annotation = returnType.getMethodAnnotation(JsonFilter.class);

を使用していたとき、私は

@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; 
} 

でクラスキャスト例外を取得していますが、私が作成している私のクラスです。

@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)); 
    } 
+0

Javaバージョンが8未満の場合は、supportメソッドのResponseBody.classをJsonFilter.classに変更してください。 – mhshimul

関連する問題