簡體   English   中英

Java Jersey Jax-RS中@QueryParam的URI匹配

[英]URI matching for @QueryParam in Java Jersey Jax-RS

如何使用查詢參數等效於此@Path參數?

@Path(“ / history / {startDate:[0-9] [0-9] [0-9] [0-9]-[0-1] [0-9]-[0-3] [0- 9]} / {endDate:[0-9] [0-9] [0-9] [0-9]-[0-1] [0-9]-[0-3] [0-9]} ”)

哪個將匹配此URI:

/history/1914-01-20/2014-01-20/

但是現在我需要匹配這個:

/history/123456?end=2024-01-20&start=2014-11-12

該代碼有效,但是沒有針對start參數和end參數的驗證:

@Path(“ / history”)

public Response getHistory(@QueryParam(“ start”)字符串開始,

                              @QueryParam("end") String end){ 

/ *** foo *** /

}

您正在使用Wich版本的JAX-RS(或Jersey)嗎?

從版本2開始,您可以使用Bean驗證( https://weblogs.java.net/blog/bhaktimehta/archive/2013/10/30/jax-rs-and-bean-validation

或者在您的示例中,實體提供商更像

Jax-RS MessageBodyReader

https://blogs.oracle.com/arungupta/entry/jax_rs_custom_entity_providers

但適用於版本JAX-RS 2。

由於有人告訴我查詢參數沒有“匹配”,所以我找到了兩種其他方法來處理此問題:

方法1:使用正則表達式

//in my includes
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//In my @GET endpoint
Pattern startPattern = Pattern.compile("[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]");
        Matcher startMatcher = startPattern.matcher(assigned);

        if (startMatcher.find()) {
            System.out.println("Matched!");
        } else {
            System.out.println("Did not match!");
        }

方法2:在您的實現中需要某種數據類型

我無法真正將其編成代碼,因為它特定於您的實現,但是在我的實現中,我需要對數據庫行使用TIMESTAMPTZ。 然后,當SQL查詢期間QueryParam不匹配時,Jax-RS會引發錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM