Spring Data Rest - How to receive Headers in @RepositoryEventHandler

限于喜欢 提交于 2019-12-03 15:30:55
Jens Schauder

You can simply autowire the request to a field of your EventHandler

@Component
@RepositoryEventHandler
public class ClientEventHandler {
    private  HttpServletRequest request;

    public ClientEventHandler(HttpServletRequest request) {
        this.request = request;
    }

    @HandleBeforeCreate
    public void handleClientSave(Client client) {
        System.out.println("handling events like a pro");
        Enumeration<String> names = request.getHeaderNames();
        while (names.hasMoreElements())
            System.out.println(names.nextElement());
    }
}

In the code given I used Constructor Injection, which I think is the cleanest, but Field or Setter injection should work just as well.

I actually found the solution on stackoverflow: Spring: how do I inject an HttpServletRequest into a request-scoped bean?

Oh, and I just noticed @Marc proposed this in thecomments ... but I actually tried it :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!