spring framework:使用不同的pathvariables处理多个请求映射的正确方法

时间:2015-05-19 20:06:07

标签: java spring

我想将多个API端点路由到同一个控制器方法。但是,我在url端点中使用pathvariables。例如:

public static AsyncResult runProcess() {

    //here I get the data from the form

    F.Promise<Boolean> callable = Akka.future(new Callable<Boolean>() {
        public Boolean call() throws Exception {

            final ProcessRunner runProcess= new ProcessRunner ();
            return runProcess.invoke(parameters);
        }
    });
    return async(
          callable.map(
            new F.Function<Boolean,Result>() {

                @Nullable
                @Override
                public Result apply(@Nullable Boolean aBoolean) {
                    return ok(createZip(parameters)); //here I download the file
                }
            })
    );
}

使用路径变量处理多个URL是否合适?是否有任何春季推荐的方法来完成这项任务?

如果存在z2,则z1为null,反之亦然?

1 个答案:

答案 0 :(得分:1)

使用

 @RequestMapping(value = {"/{x}/{y}/{cat:article|pages}/{zs:z1|z2}", method = RequestMethod.GET)
        @ResponseBody
         public Response getZ(HttpServletRequest request, HttpServletResponse response,@PathVariable("cat") String cat, @PathVariable("zs") String z){
        // you code here 
    }

如上所示,您可以为同一个路径变量建立多个值,这样可以很容易地在代码中处理这些值。希望这能回答你的问题。