Question

Here is my question

I want to intercept request before any spring controller call, check and modify request URI. After that it has to call another SPRING controller method.

I used mvc:interceptors however I want to configure it with annotations thats why I need a solution for @Aspect. Everything is working but controller called twice, for the original request and for the new request. Using interceptors I return false and it cancels it, how do I do about Aspect classes? Thank you

Here is my code:

@Component
@Aspect
public class TestAspect {

    @Before("execution(* mycontroller.*(..))  &&" + "args(request,response)")
    public void interceptUrl(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("@Aspect is running!");
        System.out.println(request.getRequestURI());

        if (request.getAttribute("client") == null) {
            request.setAttribute("client", "test");
            request.getRequestDispatcher("/newpath/contact").forward(request, response);
        }

    }

}
Was it helpful?

Solution

You should consider @Around advice instead of @Before. In this case you can simply not execute original request chain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top