1

I'm writing a Web service, using Jersey 2.17 with Tomcat 8.

In the web.xml when I change my url pattern to /* I'm able to hit the web-service and get response. However when I change the url pattern to / then I always get 404 Error (resource not found).

Doesn't work

<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/</url-pattern> 
</servlet-mapping>

Work's Perfectly

<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/*</url-pattern> 
</servlet-mapping>

My web.xml contains no other Servlet or Filter mappings apart from this.

<servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>
             org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
             <param-value>com.bandi.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

Is there any other configuration needed to make this work? I cannot afford to use /* as the servlet mapping because I need certain Filters to run before this servlet gets executed. If the filter fails then the call to servlet should not go ahead.

So basically I don't want to override any mappings that are already present.

Kishore Bandi
  • 5,537
  • 2
  • 31
  • 52
  • @BalusC I'm converting existing (300+) Spring to Jersey services. Spring's **"servlet.DispatcherServlet"** allowed using default / url-pattern. The problem with defining /api/* is that these 300+ services are being used by many projects and it requires a change in their end as well. i.e., Changing URL to **http://someweb.com/api/mywebservice** instead of existing **http://someweb.com/mywebservice**. So I cannot change the contract that we've with other services. Why can't Jersey have this feature? It can check if any of the Path's defined matches the current URI Path. If not it can throw 404. – Kishore Bandi Mar 01 '16 at 12:45
  • I'm going ahead with changing the request URL at NGINX to add additional characters to the request, so I can go ahead with /rest/* as the url-pattern. – Kishore Bandi Mar 01 '16 at 14:04
  • Great. I reposted it as an answer. – BalusC Mar 01 '16 at 14:12
  • See also http://jersey.576304.n2.nabble.com/Jersey-and-url-pattern-td4548025.html – rogerdpack Oct 12 '17 at 17:59

1 Answers1

0

Try to use filter instead of servlet in web.xml like this -

<filter-mapping>
    <filter-name>jersey-servlet</filter-name>
    <url-pattern>/</url-pattern>
</filter-mapping>
RSatpute
  • 59
  • 1
  • 1
  • 8
  • @R Satpute I tried using Jersey ServletContainer as Filter as well. But even that Didn't work. Referred to : https://jersey.java.net/documentation/latest/deployment.html – Kishore Bandi Mar 01 '16 at 10:07
  • Yes, If not the servers wouldn't start as its not a sub-tag of Filter tag. – Kishore Bandi Mar 01 '16 at 10:31
  • 1
    Next time, please try yourself before doing off as an answer. Bonus is, you'll really learn something by experience this way. – BalusC Mar 01 '16 at 14:10
  • I am having my own project in this scenario, AND YES I TRIED IT BEFORE POSTING THE ANSWER. – RSatpute Mar 02 '16 at 06:28
  • And yes, you can only access base URL by using /, if you want to access particular path in app, you need to have pattern like /*, or else it will show 404. – RSatpute Mar 02 '16 at 06:30