Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

java - Intercept the instance of an object from RestController or Hibernate in Spring before it's returned to the client

I'm developing a translation service that currently works inside another Service. For example:

public Profile getById(int chainId, int profileId, Integer languageId) {
    Profile profile = profileRepository.getById(chainId, profileId);
    translationService.translate(profile, languageId); // Here
    return profile;
}

Now, to avoid to use a translate method on every service method of all the application, and as I only have the language of a user from the controller, I would like to execute the translate method before every Profile (and any other object) is returned to the client.

I tried to implement HandlerInterceptor in a custom interceptor, but it seems it doesn't returns the instance of the object that I'm returning. Anyone could help?

Another way to do it could be to translate every object that came from a select in Hibernate, but I also don't find any good solution to it this way...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution was to use Spring AOP. Probably the question wasn't very well explained, but what we needed was a way to intercept the object a user was asking to the backend, because they are able to create their own translations and we save them in the database. We had to return the model with the correct translation for each user, who has their localization in their profile. Here's the way we intercept it:

@Component
@Aspect
public class TranslatorInterceptor extends AccessApiController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public TranslationService translationService;

    @Pointcut("execution(* com.company.project.api.controller.*.get*(..))")
    public void petitionsStartWithGet() { }

    @Pointcut("execution(* com.company.project.api.controller.*.list*(..))")
    public void petitionsStartWithList() { }

    @Pointcut("execution(* com.company.project.api.controller.*.find*(..))")
    public void petitionsStartWithFind() { }

    @AfterReturning(pointcut = "petitionsStartWithGet() || petitionsStartWithList() || petitionsStartWithFind()", returning = "result")
    public void getNameAdvice(JoinPoint joinPoint, Object result){
        translationService.translate(result, getCustomUserDetails().getLanguageId());
        logger.debug("Translating " + result.getClass().toString());
    }
}

What we do here is to "watch" all the methods in the package "controller" that start by 'get', 'list' or 'find' (getById(), for example) and through this advice, we intercept the object before is sent to Jackson. The method getCustomUserDetails comes from AccessApiController, which is a class we did to provide our Controllers with some information we need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...