Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a default method for unmatched REST methods in Jersey

Tags:

rest

jersey

I know this is not quite "restful" but, I want to learn if and how I can handle all requests which do not match any of the methods in my REST resource (I want to proxy these requests to another server). For example, it could be a method like:

@GET 
@Path("*") 
public Response defaultMethod(@Context HttpServletRequest request, @Context HttpServletResponse response) 
{ 
    // do proxying here 
} 

how can I achieve this?

BR,

SerkanC

like image 790
Serkan Camurcuoglu Avatar asked Dec 21 '22 21:12

Serkan Camurcuoglu


2 Answers

One possible option would be to define a custom 404 mapping. Since 404s handles all unmatched requests it should catch all undefined urls.

I added the following this to my web.xml file

 <error-page>
    <error-code>404</error-code>
    <location>/error/404</location>
  </error-page>

Where location is the path you want to direct the request to. Then just define that the call as normal.

like image 29
Chris Salij Avatar answered Dec 25 '22 12:12

Chris Salij


@Path("/{default: .*}")

This works for:

  • http://example.com/someUnexpectedPath
  • http://example.com/someUnexpectedPath/withAdditionalSubPaths
  • http://example.com/someUnexpectedPath/withAdditionalSubPaths?andParams=whatever
like image 128
Matthew Kuraja Avatar answered Dec 25 '22 12:12

Matthew Kuraja