Adding business logic to spring-data-rest application

I experimented with spring-data-rest (SDR) and am really impressed with how quickly I can build a rest api. My application is based on the following repository, which gives me GET / attachements and POST / attachements

package com.deepskyblue.attachment.repository;

import java.util.List;

import org.springframework.data.repository.Repository;

import com.deepskyblue.attachment.domain.Attachment;

public interface AttachmentRepository extends Repository<Attachment, Long> {

    List<Attachment> findAll();

    Attachment save(Attachment attachment);
}

One thing I'm confusing is how I add custom business logic. The SDR seems great if I just want to use the remainder API for my data, however a traditional Spring application usually has a service level where I can have business logic. Is there any way to add this business logic to the SDR?

+4
source share
3 answers

, - 3 , .

, . , [@RestController]. , .

, , Autowire - - .

ou RepositoryRestController. Spring Data RESTs, , ..,

, , .

+1

Aspect, . - (groovy):

@Aspect
@Component
@Slf4j
class AccountServiceAspect {

@Around("execution(* com.test.accounts.account.repository.AccountRepository.save*(..))")
    Object saveAccount(ProceedingJoinPoint jp) throws Throwable {
        log.info("in aspect!")
        Object[] args = jp.getArgs()

        if (args.length <= 0 || !(args[0] instanceof Account))
            return jp.proceed()

        Account account = args[0] as Account

        account.active = true
        jp.proceed(account)
    }
}

, , spring .

+1

Source: https://habr.com/ru/post/1673678/


All Articles