I want to execute a JSON response received from adding a controller. status attribute. In this regard, I am going to use the Aspect class, which the @Around method returns a custom class object. In this case, I get an error:
java.lang.ClassCastException: *.controller.RestResponse cannot be cast to java.util.List
Is there a way to change the return of type @ResponseBody to a custom type through aspectJ @Around annotation? I canβt change the controller code!
Controller Class:
@Controller @RequestMapping(value = "/users") public class UserController { @Autowired private UserService userService; @RequestMapping(method = RequestMethod.GET) @ResponseBody public List<User> get() throws InterruptedException { return userService.getUsers(); } ... }
Aspect Class:
@Component @Aspect public class RestInterceptor { @Pointcut("within(* controller.api.*)") public void endpointMethod() { } @Around("endpointMethod()") public RestResponse unifyResponse(ProceedingJoinPoint pjp) throws Throwable { Object controllerResult = pjp.proceed(); RestResponse result = new RestResponse(0, controllerResult); return result; } }
Custom RestResponse Class:
public class RestResponse{ private int status; private String message; private Object data; public RestResponse(int status, Object data) { this.status = status; this.data = data; } public RestResponse(int status, String message) { this.status = status; this.message = message; }
source share