简体   繁体   中英

How the annotation @ApplicationPath from JAX-RS works side to side with a non-rest resource?

I've been creating a restful api and I had some issues that I couldn't find anything about it over the internet.

What happens if I have a restful api with an applicationPath pointing to "/" and some non-rest resource on the same project? The same question is valid to static content that I've added to a project and it got ignored.

Let's use a domain foo.com and the context name bar as example:


I've created a rest interface with an @applicationPath pointing to "/" and a resource with a @path pointing to publications .

So I'd have foo.com/bar/publications as part of the restful api, and it would be functional.

But if I have a static content like api-doc , which should appear on foo.com/bar/api-doc it's just ignored.

The same happens with a non-rest api, like a simple java's servlet.. Everything after the slash that's not a rest resource is ignored.


Is there a way of making them work together using this applicationPath pointing to "/" or is it impossible? Because I can't find anything about it on the documentation. I just want to understand how it works to make a better use of it.

Might be of relevance to others running into these kinds of issues.

It sounds like you have a situation of URL path shadowing. Consider the order in which you declare your mappings.

From Servlet 3 spec, section 12.1 :

The first successful match is used with no further matches attempted:

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.

  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the '/' character as a path separator. The longest match determines the servlet selected.

  3. If the last segment in the URL path contains an extension (eg .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last '.' character.
  4. Versions of this specification prior to 2.5 made use of these mapping techniques as a suggestion rather than a requirement, allowing servlet containers to each have their different schemes for mapping client requests to servlets. 116 Java Servlet Specification • November 2009
  5. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used. Many containers provide an implicit default servlet for serving content.

Alternatively, you might be inadvertently overriding your @ApplicationPath with a servlet-mapping element in your web.xml

Hope some will find this useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM