Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResponseBodyAdvice should ignore non-match type instead throw exception [SPR-17539] #22071

Closed
spring-projects-issues opened this issue Nov 27, 2018 · 1 comment
Assignees
Labels
in: web status: declined type: enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Nov 27, 2018

abccbaandy opened SPR-17539 and commented

When I use 

public class MyAdvice implements ResponseBodyAdvice<MyVo>

and my response data is

ResponseEntity<AnotherVo>

ResponseBodyAdvice will throw ClassCastException, because we can't use "supports" to check the type. It make type declare in ResponseBodyAdvice interface useless.

 

Currently, there is a workaround, use Object

ResponseBodyAdvice<Object>

and check the type in beforeBodyWrite, like this

@Override
public Object beforeBodyWrite(Object o, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    if (!(o instanceof MyVo)) {
        return o;
    }
    // do rest things here
}

but it doesn't look like a best practice.

 

update:

after study the source, I found there is a class cast before call beforeBodyWrite

RequestResponseBodyAdviceChain

private <T> Object processBody(@Nullable Object body, MethodParameter returnType, MediaType contentType,
      Class<? extends HttpMessageConverter<?>> converterType,
      ServerHttpRequest request, ServerHttpResponse response) {

   for (ResponseBodyAdvice<?> advice : getMatchingAdvice(returnType, ResponseBodyAdvice.class)) {
      if (advice.supports(returnType, converterType)) {
         body = ((ResponseBodyAdvice<T>) advice).beforeBodyWrite((T) body, returnType,
               contentType, converterType, request, response);
      }
   }
   return body;
}

Can we check body is T before call beforeBodyWrite?


Affects: 5.0.9

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Nov 28, 2018

Rossen Stoyanchev commented

ResponseBodyAdvice will throw ClassCastException, because we can't use "supports" to check the type.

You can use the supports method to check. MethodParameter has the generic type information, e.g.:

boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
    Class<?> targetType = ResolvableType.forMethodParameter(returnType).getGeneric(0);
    // ...
}

 

Can we check body is T before call beforeBodyWrite?

Not really. We don't have generic type information for the advice bean at that point.
 

@spring-projects-issues spring-projects-issues added status: declined type: enhancement in: web labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web status: declined type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants