Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RepositoryEventHandler events stop with @RepositoryRestController

When I create a @RepositoryRestController for an entity, the associated @RepositoryEventHandler methods are not triggered in Spring Data REST via Spring Boot 1.4.0.M3 (also Spring Boot 1.3.5) -- is this a bug, or as designed?

I have an Account entity with an @RepositoryEventHandler:

@Slf4j
@Component
@RepositoryEventHandler(Account.class)
public class AccountEventBridge {

    @HandleBeforeCreate
    public void handleBeforeCreate(Account account){
        log.info("Before create " + account);
    }

    @HandleAfterCreate
    public void handleAfterCreate(Account account){
        log.info("Created " + account);
    }
}

which trigger as they should when I POST:

curl -H "Content-Type: application/json" -X POST 
  -d '{"name":"aaa", "owner":{"email":"aaa@1010","password":"snap"}}'
  http://localhost:8080/api/accounts

unless I add a @RepositoryRestController:

@RepositoryRestController
public class AccountRespositoryRestController {

    private final AccountRepository repository;

    @Autowired
    public AccountRespositoryRestController(AccountRepository repository) {
        this.repository = repository;
    }

    @RequestMapping(method = RequestMethod.POST,value = "/accounts")
    public @ResponseBody PersistentEntityResource post(
        @RequestBody Account account,
        PersistentEntityResourceAssembler assembler) {

        // ...
        Account entity = this.repository.save(account);
        return assembler.toResource(entity);
    }
}

When I comment out the @RepositoryRestController annotation, the @RepositoryEventHandler methods trigger, again.

It seems like these should behave independently since they operate a two different conceptual layers within Spring Data REST -- or am I misunderstanding something?

If this is intentional, it's unfortunate -- I'll have to implement all HTTP methods to create the events myself for any entity with an @RepositoryRestController. Is that really the intent?

like image 308
Jan Nielsen Avatar asked Jun 02 '16 22:06

Jan Nielsen


1 Answers

It's as implemented. :-)

The methods defined in a @RepositoryRestController implementation replace the methods in the default RepositoryEntityController which publish @RepositoryEventHandler events.

But it's easy to add these events making the @RepositoryRestControll a ApplicationEventPublisherAware implementation and publishing the events like the default RepositoryEntityController implementation:

@Slf4j
@RepositoryRestController
@AllArgConstructor
public class AccountRespositoryRestController 
    implements ApplicationEventPublisherAware {

    private final AccountRepository repository;
    private ApplicationEventPublisher publisher;

    @Override
    public void setApplicationEventPublisher(
        ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    @RequestMapping(method = RequestMethod.POST,value = "/accounts")
    public @ResponseBody PersistentEntityResource post(
        @RequestBody Account account,
        PersistentEntityResourceAssembler assembler) {

        // ...
        publisher.publishEvent(new BeforeCreateEvent(account));
        Account entity = this.repository.save(account);
        publisher.publishEvent(new AfterCreateEvent(entity));

        return assembler.toResource(entity);
    }
}

You can also inject the publisher without making the class ApplicationEventPublisherAware:

@Slf4j
@RepositoryRestController
@AllArgConstructor
public class AccountRespositoryRestController {

    private final AccountRepository repository;
    private final ApplicationEventPublisher publisher;

    @RequestMapping(method = RequestMethod.POST,value = "/accounts")
    public @ResponseBody PersistentEntityResource post(
        @RequestBody Account account,
        PersistentEntityResourceAssembler assembler) {

        // ...
        publisher.publishEvent(new BeforeCreateEvent(account));
        Account entity = this.repository.save(account);
        publisher.publishEvent(new AfterCreateEvent(entity));

        return assembler.toResource(entity);
    }
}
like image 81
Jan Nielsen Avatar answered Oct 02 '22 01:10

Jan Nielsen