我有一个FooServiceImpl
bean,它使用特定的表单包装来自rest控制器的返回对象:
ResponseBodyAdvice
但问题是控制器中的public class Response<T> {
private T data; //the data is from the rest controller
private int code;
private String message;
}
@RestControllerAdvice
public class ResponseAdviceConfig implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof Response) {
return body;
} else {
return new Response<>(body);
}
}
}
@Data
public class User {
@JsonView(SummaryView.class)
private String account;
@JsonView(SummaryView.class)
private String avatar;
private String realname;
@JsonView(SummaryView.class)
private String nickname;
private String password;
public interface SummaryView {
}
}
@RestController
@RequestMapping("/v1/user")
public class UserAPI {
@GetMapping("/follows/list")
@JsonView(User.SummaryView.class)
public List<User> followsList(@RequestParam String account){
return userService.followsList(account);
}
}
带注释的方法不再起作用,最终响应只是一个空对象@JsonView
。我猜这个{}
bean可能与内部ResponseBodyAdvice
冲突。我没有结果但是没有结果。