2

I want to write filter, and get client httprequest before controller and make some code, depends on URL.

Request can be: HttpRequest, MultipartHttpServletRequest, can be POST or GET. I need to make request to another REST API, if the URL of this request starts with api.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
annoirq
  • 825
  • 5
  • 18
  • 30
  • Sure that will work. Just implement "[Filter](http://stackoverflow.com/questions/2725102/how-to-use-a-servlet-filter-in-java-to-change-an-incoming-servlet-request-url)" from the servlet api and map it to your Spring dispatcher servlet. Or you might keep using a Spring controller (avoiding a filter) and write a rest client to connect to the foreign system. Id prefer the latter. – Stefan Oct 20 '15 at 06:27
  • I tried to use controllers. But I have path with slashes, and it not working for it. For example user sent request "http://myserver/api/email/1". I need to write in controller "value = "api/{path}"". Path should be equal "email/1", but path cannot contain slash. I don't know how many slashes can send client inside httprequest – annoirq Oct 20 '15 at 06:41

1 Answers1

5

You should use Spring org.springframework.web.servlet.HandlerInterceptor

(hopefully this answer explain how to use it)

(or you could use an simple Servlet-Filter - see also this question Spring HandlerInterceptor vs Servlet Filters it discuss the difference between them)

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383