Case-insensitive URLs with JAX-RS

10,404

Solution 1

The answer is No, since basically URIs according to RFC 3986 are case sensitive:

6.2.2.1. Case Normalization

For all URIs, the hexadecimal digits within a percent-encoding triplet (e.g., "%3a" versus "%3A") are case-insensitive and therefore should be normalized to use uppercase letters for the digits A-F.

When a URI uses components of the generic syntax, the component syntax equivalence rules always apply; namely, that the scheme and host are case-insensitive and therefore should be normalized to lowercase. For example, the URI is equivalent to http://www.example.com/. The other generic syntax components are assumed to be case-sensitive unless specifically defined otherwise by the scheme (see Section 6.2.3).

If you still want to make them case insensitive, you can use a servlet filter and put it in front of JAX-RS framework. You still need to be consistent in your application.

If you consider switching from Jersey to Apache Wink, you can use the Dynamic Resources to ensure that all urls are lower/upper-cased. So combining a servlet filter with the Dynamic Resources can be a full solution for this case.

Solution 2

I think I found simple solution which is JAX-RS specification compliant. You are able to use in @Path annotation regular expression like this:

@Path("/{message:[mM][eE][sS][aA][gG][eE]}")

I have used it with JBoss RESTeasy implemention. See more there.

Share:
10,404

Related videos on Youtube

Matt Ball
Author by

Matt Ball

Updated on May 28, 2022

Comments

  • Matt Ball
    Matt Ball almost 2 years

    Is there any easy way to provide a case-insensitive URLs in a JAX-RS web service? The goal of this is to produce a web service which is a "lenient acceptor."1

    I imagine it's possible to do this with a filter which .to[Lower|Upper]Case()s all incoming URLs. Unfortunately, this implementation demands programmer discipline/consistency in making sure that all hard-coded URL strings in the application are strictly [lower|upper]case.
    Also, I don't yet know the JAX-RS analog to a servlet filter.

    If it matters, I'm using Jersey as my JAX-RS implementation.


    1As in, "be lenient in what you accept, and strict in what you produce" (can't recall the source)

  • dactylroot
    dactylroot over 9 years
    the case-insensitive flag works for me with Jersey 2.10 @Path( "/{message : (?i)message}" )
  • dactylroot
    dactylroot over 9 years
    The answer depends on your definition of "easy"