Search code examples
spring-rooaop

How to change the returned result of a Spring Roo function


I am using Spring Roo for my restFull web services. Working great. Now I want to change the default behaviour on all instances. When a post is done I need the resultant record Id returned. The Roo code is

@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<String> MdrAspectController.createFromJson(@RequestBody String json) {
    MdrAspect mdrAspect = MdrAspect.fromJsonToMdrAspect(json);
    mdrAspectService.saveMdrAspect(mdrAspect);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json");
    return new ResponseEntity<String>(headers, HttpStatus.CREATED);
}

I would like to change the return to

    return new ResponseEntity<String>(mdrAspect, headers, HttpStatus.CREATED);

meaning it gives back the created record (as I need the record id).

I would like to write an aspect to do this for all Roo_Controller_Json.aj. But it is already an aspect. Is this possible?


Solution

  • Yes, it is possible to apply aspects on other aspects. This is even a typical trap that many beginners run into because they forget to exclude their own aspect from its pointcuts, which can lead to advice being called in an endless loop resulting in a stack overflow - nice word to use here. ;-)

    Yes, it is possible to change a method's return value by means of an around() advice.

    No, it is impossible to access values of local variables from within an aspect. So in your example if you want to access the value of mdrAspect you need to get it from another source or create an instance by yourself.

    Please tell me if you have more concrete questions.