Data AccessCore JavaApp FrameworksViewTestingBuildingDeploymentDev ToolsSecurityOpinions

Saturday, November 6, 2010

JAX-RS @Path Precedence Rules

The other day I needed to figure out how the JAX-RS provider resolves ambiguous path expressions passed in the @javax.ws.rs.Path annotation. I learned about the provider's precedence rules and wanted to document them here with a short example.

For example, say we have the following @javax.ws.rs.Path expressions in the following class:

@Path("/users") 
public class UserResource {

  @GET
  @Path("{id : .+}")
  public String getUser(@PathParam("id") String id){
    ....
  }


  @GET
  @Path("{id : .+}/address")
  public String getAddress(@PathParam("id") String id){
    ....
  }
}

Note that .+ will match any stream of characters after "/users".
Now, suppose the following GET request was submitted.

GET /users/32/address

The request actually matches both expressions, but the request would be routed to the getAddress(@PathParam("id") String id) method. The JAX-RS provider determines which method to call based on a set of precedence rules. On deployment, the JAX-RS provider gathers and sorts all URI expressions contained within all known @Path annotations based on the following logic:

  1. The number of literal characters contained within the expression, sorted in descending order. In the example above, the getAddress(@PathParam("id") String id) method gets precedence over the getUser(@PathParam("id") String id) method. The getUser(@PathParam("id") String id) method contains 7 literal characters (/users/). The getAddress @PathParam("id") String id) method contains 14 literal characters (/users/ + address).
  2. The number of template expressions within the expression, sorted in descending order. For example, an expression containing {id}/{name} would get precedence over {id}.
  3. Finally, the number of regular expressions contained within the expression. For example, {id : .+} would get precedence over {id}.

In closing, the following URI expressions are sorted by the order of precedence above.

  1. /users/{id}/{name}/address
  2. /users/{id : .+}/address
  3. /users/{id}/address
  4. /users/{id : .+}

Any questions, leave a comment and I'll be sure to answer.

12 comments:

  1. good explanation.

    ReplyDelete
    Replies
    1. quay lại Đấu khí học viện. Tử Điện Mãng đưa cho Khiếu Thiên Hổ mấy viên đan dược, đây chính là Nhạc Thành lưu lại cho hắn.

      Tư Mã Yên Nhiên, Tô Hân Nhi, Đỗ Mật Nhi, Âu Dương Phiên Tình, bốn nười cũng bế quan trong phòng tu luyện bảy ngày, trong bốn nữ tử thì chỉ có Tư Mã Yên Nhiên vốn có thẻ linh nửa tháng, nhưng sau đó Nhạc Thành đã đưa cho bốn cô gái số thẻ linh gấp ba. đồng tâm
      game mu
      cho thuê phòng trọ
      cho thuê phòng trọ
      nhac san cuc manh
      tư vấn pháp luật qua điện thoại
      văn phòng luật
      số điện thoại tư vấn luật
      dịch vụ thành lập doanh nghiệp

      Nhìn thấy thực lực của Đỗ Mật Nhi, Âu Dương Phiên Tình, Tô Hân Nhi còn cao hơn cả mình, Tư Mã Yên Nhiên cũng ganh đua trong lòng.

      Danh khí của Phá Quân Bang hiện tại đã lên rất cao, một số tam tinh đấu vương, thậm chí là tứ tinh Đấu vương cũng gia nhập. Chỉ là Nhạc Thành đã để lại lời nói, muốn gia nhập Phá quân bang thì sau này rời khỏi đấu khí học viện vẫn phải là người của Phá Quân Bang, nếu không chịu thì sẽ không được gia nhập, điều này khiến cho rất nhiều người không muốn vào nữa, tuy nhiên hiện tai Phá Quân Bang cũng đã có hơn một trăm người.

      Mấy hôm trước, Âu Dương Phiên Tình, Đỗ Mật Nhi còn có Tư Mã Yên Nhiên luyện chế ra không ít đan dược bán cho Phá Quân Bang, những đan dược này đều là đan dược tứ phẩm, bán giá trị rất cao.

      Delete
  2. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

    Online Java Training Java Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai

    ReplyDelete
  3. On the off chance that you don't have credit, or you have terrible credit, and you don't have the money, how is that bill paid? If not for payday advances, that bill wouldn't be paid.

    ReplyDelete
  4. So in the event that you require money quick, a bank advance is not for you. Bank credits work preferred for arranged costs over for unanticipated money related crises.

    ReplyDelete
  5. So what do you do? Given the innovative headway, you can essentially sign on to an online fund website that gives quick loans.

    ReplyDelete
  6. Once the business goes ahead, you can either apply for more advances online to use as extra financing or essentially pay off your obligation.

    ReplyDelete
  7. Thank you for sharing the post! This is what I need to find.
    instagram viewer

    ReplyDelete
  8. CIITN is the Best Php training institute in Noida and delhi Ncr. You will get Live Project Training on PHP by our PHP expert who have 5+ year industrial experience.Focus on practical and live project training. In our PHP training, we you will learn core PHP, advance PHP, HTML, CSS, JavaScript, jQuery, Bootstrap, Cake PHP and Wordpress.CIITN provides 100% job assistance in PHP training. CIITN is well known PHP coaching center because our 100% PHP students are placed now.


    Ciitnoida provides Core and java training institute in noida. We have a team of experienced Java professionals who help our students learn Java with the help of Live Base Projects. The object-oriented, class-based build of Java has made it one of most popular programming languages and the demand of professionals with certification in Advance Java training is at an all-time high not just in India but foreign countries too.

    By helping our students understand the fundamentals and Advance concepts of Java, we prepare them for a successful programming career. With over 13 years of sound experience, we have successfully trained hundreds of students in Noida and have been able to turn ourselves into an institute for best Java training in Noida.


    php training in noida
    java training institute in noida
    javascript training in noida
    linux training in noida
    linux institute in noida
    red hat linux training in noida

    ReplyDelete