简体   繁体   中英

JAX-RS Path Matching

I would like to validate URLs for my REST API. For example I have this JAX-RS Path:

/users/{id : \\d+} 

Now I will test different URLs without starting the server

/users/1 -> valid

/users/a -> not valid

Anybody know an existing solution or possibilty to do it?

Update:
I created a method for getting Regex from Path Annotation. But there could be problems when the chars { , } or : are on an unexpected position.

private String GetRegex(String patternPath) {
    int indexOfOpen = patternPath.indexOf("{", 0);
    if (indexOfOpen < 0) {
        return patternPath;
    } else {
        int indexOfClose = patternPath.indexOf("}");
        int indexOfRegexStart = patternPath.indexOf(":");
        if (indexOfRegexStart < 0 || indexOfRegexStart > indexOfClose) {
            patternPath = patternPath.substring(0, indexOfOpen) + ".*"
                    + patternPath.substring(indexOfClose + 1, patternPath.length());
        } else {
            patternPath = patternPath.substring(0, indexOfOpen)
                    + patternPath.substring(indexOfRegexStart + 1, indexOfClose).trim()
                    + patternPath.substring(indexOfClose + 1, patternPath.length());
        }

        return GetRegex(patternPath);
    }
}

Test:

String patternPath = "/users/{userid}/{userId2 : \\d+$}";
String methodPath = "/users/435345/435435";

String getRegex = GetRegex(patternPath);

Assert.assertEquals("/users/.*/\\d+$", getRegex);

Matcher matcher = Pattern.compile(getRegex).matcher(methodPath);
Assert.assertEquals(true, matcher.matches());

The regex \\d+$ matches 1 to N digits and that the string ends with it. It will not match a characte a for example.

You can do your code something like it:

Matcher matcher = Pattern.compile("/users/\\d+$").matcher(string);
if (matcher.find()) {
    ...
}

I improved my method. I am searching now for the template expression with "/{" and for the end of template expression with "}/" .

public String GetRegex(String patternPath) {

    int indexOfOpen = patternPath.indexOf("/{");
    if (indexOfOpen < 0) {
        return patternPath;
    } else {
        int indexOfClose = patternPath.indexOf("}/");
        if(indexOfClose < 0){
            if(patternPath.substring(patternPath.length() - 1).equals("}")){
                indexOfClose = patternPath.length() - 1;
            }
            else{
                return patternPath;
            }
        }

        int indexOfRegexStart = patternPath.indexOf(":", indexOfOpen);
        if (indexOfRegexStart < 0 || indexOfRegexStart > indexOfClose) {
            patternPath = patternPath.substring(0, indexOfOpen + 1) + ".*" + patternPath.substring(indexOfClose + 1, patternPath.length());
        } else {
            patternPath = patternPath.substring(0, indexOfOpen + 1)
                    + patternPath.substring(indexOfRegexStart + 1, indexOfClose).trim()
                    + patternPath.substring(indexOfClose + 1, patternPath.length());
        }

        return GetRegex(patternPath);
    }
}

Test:

String itemPath = "/users/{userid}/{userId2 : \\d+:{$}/{userid3}/{userid4 : [:]}/{userId5}/{userId6 : [8]}";
String getRegex = GetRegex(itemPath);
Assert.assertEquals( "/users/.*/\\d+:{$/.*/[:]/.*/[8]", getRegex);

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