@RepositoryRestController

时间:2018-01-16 16:51:19

标签: java spring spring-mvc spring-data-rest

我正在使用Spring Boot,Spring Data REST,Spring HATEOAS。我创建了一个@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);
    }

}

我需要在持久保存Ticket对象之前拦截事件。我创建了我的处理程序:

@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);
        }
    }

}

这是我的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);

}

documentation中所述,但未发出事件。就像描述here我正在使用@HandleBeforeCreate注释。我做错了吗?

0 个答案:

没有答案