Wednesday, 14 January 2015

Spark java : Named Parameters and wild cards


URL patterns can include named parameters, accessible via the params method of the request object.

import static spark.Spark.*;

public class HelloSpark {
  public static void main(String[] args) {  
    get("/hello/:name", (request, response) -> {
        return "Hello: " + request.params(":name");
    });
  }

}


Run HelloSpark and View below url


URL Patterns can also include splat (or wildcard) parameters. These parameters can be accessed by using the splat method on the request object.

import static spark.Spark.*;

public class HelloSpark {
  public static void main(String[] args) {  
    get("/hello/*/welcome/*", (request, response) -> {
        
        String splats[] = request.splat();
        String str = "Number of splat parameters: " + request.splat().length;
        str = str + "<br /> splat parameters are ";
        
        for(String s : splats){
          str = str + s + "  ";
        }
        
        return str;
    });
  }

}


For the URL

Response contain below data
Number of splat parameters: 2
splat parameters are hari spark




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment