0

I'm using Spring Boot, Spring Data REST, Spring HATEOAS. I created a @RepositoryRestController:

@Api(tags = "Ticket Entity")
@RepositoryRestController
@PreAuthorize("isAuthenticated()")
public class TicketController extends RevisionController<TransitCertificate> {
    private Logger log = LogManager.getLogger();

    @Autowired
    private LocalValidatorFactoryBean validator;

    @Autowired
    private TicketService ticketService;

    @Autowired
    private EnumTranslator enumTranslator;

    @SuppressWarnings("rawtypes")
    @Autowired
    private PagedResourcesAssembler pagedResourcesAssembler;

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private Javers javers;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.addValidators(validator);
    }

    @PostMapping(path = "/tickets")
    public ResponseEntity<?> save(@RequestBody(required = true) @Valid Ticket ticket, PersistentEntityResourceAssembler resourceAssembler) {
        return new ResponseEntity<>(resourceAssembler.toResource(ticketService.save(ticket)), HttpStatus.OK);
    }

}

I need to intercept the event before the Ticket object is persisted. I created my handler:

@Component
@RepositoryEventHandler(Ticket.class)
public class TicketHandler {
    private Logger log = LogManager.getLogger();

    @Autowired
    private WorkShiftRepository workShiftRepository;

    @HandleBeforeCreate
    public void handleBeforeCreates(Ticket ticket) {
        WorkShift workShift = workShiftRepository.findByAgentUsernameAndEndDateIsNull();
        if (workShift != null) {
            ticket.setWorkShift(workShift);
        }
    }

}

and this is my TicketRepository:

@Transactional
@PreAuthorize("isAuthenticated()")
public interface TicketRepository extends PagingAndSortingRepository<Ticket, Long> {

    @RestResource(exported = false)
    @Override
    public <S extends Ticket> Iterable<S> save(Iterable<S> entities);

    @RestResource(exported = false)
    @Override
    public <S extends Ticket> S save(S entity);

    @RestResource(exported = false)
    @Override
    public void delete(Long id);

    @RestResource(exported = false)
    @Override
    public void delete(Ticket entity);


    @Query(value = "SELECT MAX(number) FROM Ticket t WHERE t.block=:ticketBlock")
    public Long findMaxNumber(@Param("ticketBlock") TicketBlock ticketBlock);

}

as described in the documentation but the event is not emitted. Like described here I'm using the @HandleBeforeCreate annotation. Am I doing something wrong?

drenda
  • 5,846
  • 11
  • 68
  • 141
  • can you add the ticket repository code? – ESala Jan 16 '18 at 17:40
  • @ESala done. Thanks – drenda Jan 16 '18 at 18:04
  • Not sure about this, but it may be that events are not emitted when you add your own request mappings, such as your custom `POST /tickets`. Can you try doing the same but using the mappings created by the repository? – ESala Jan 16 '18 at 18:13
  • @ESala I'm sure with the Repository it works. I already did it for other entities and it worked. Like I wrote the problem is make it works also with my custom RepositoryRestController. Thanks – drenda Jan 16 '18 at 18:35
  • 1
    I'm not sure this is possible with custom controllers. Maybe someone else can clarify this. One solution would be to publish your own events. See this blog post: https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2 – ESala Jan 16 '18 at 19:39

0 Answers0