在将对象返回给客户端之前,先从Spring中的RestController或Hibernate截取该对象的实例 [英] Intercept the instance of an object from RestController or Hibernate in Spring before it's returned to the client

查看:104
本文介绍了在将对象返回给客户端之前,先从Spring中的RestController或Hibernate截取该对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种翻译服务,该服务目前可在另一个服务中使用.例如:

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

现在,为避免在所有应用程序的每个服务方法上都使用转换方法,并且由于我只有控制器用户的语言,因此我想在每个Profile(以及任何其他配置文件)之前执行translate方法.对象)返回给客户端.

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.

我尝试实现 HandlerInterceptor 在自定义拦截器中,但似乎没有返回我要返回的对象的实例.有人可以帮忙吗?

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?

另一种实现方法可能是转换Hibernate中来自select的每个对象,但是我也没有找到这种方法的任何好的解决方案...

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...

推荐答案

解决方案是使用Spring AOP.可能问题没有得到很好的解释,但是我们需要的是一种拦截用户向后端询问的对象的方法,因为他们能够创建自己的翻译并将其保存在数据库中.我们必须为每位用户返回正确的翻译模型,这些用户的个人资料已在其个人资料中进行了本地化.这是我们拦截它的方式:

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

我们在这里要做的是监视"程序包"controller"中所有以"get","list"或"find"(例如,getById())开头的方法,并通过此建议来拦截之前的对象被发送到杰克逊.方法getCustomUserDetails来自AccessApiController,这是我们为控制器提供所需信息的类.

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.

这篇关于在将对象返回给客户端之前,先从Spring中的RestController或Hibernate截取该对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆