Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在单个PathVariable中接受多个参数_Java_Spring_Spring Mvc_Post - Fatal编程技术网

Java 在单个PathVariable中接受多个参数

Java 在单个PathVariable中接受多个参数,java,spring,spring-mvc,post,Java,Spring,Spring Mvc,Post,我有一个控制器方法,如下所示: @PostMapping("/view/{location}") public ModelAndView view(@PathVariable("location") String location) { ModelAndView modelAndView = new ModelAndView(); return modelAndView; } 此方法能够接收以下请求: /查看/a或/view/b,使路径变量位置变为a或b 但我希望使用相同的方

我有一个控制器方法,如下所示:

@PostMapping("/view/{location}")
public ModelAndView view(@PathVariable("location") String location) {

    ModelAndView modelAndView = new ModelAndView();
    return modelAndView;
}
此方法能够接收以下请求:

/查看/a或/view/b,使路径变量位置变为a或b

但我希望使用相同的方法来接收开始时具有/view的所有请求,以便pathVariable位置保存其余数据

比如说

对于作为/view/a/b/c的请求,pathVariable位置将变为a/b/c

类似于文件系统层次结构

请让我知道这样的事情在SpringMVC中是否可能发生,我对这一点非常陌生。

看看这个

其思想是通过添加**将以/view开头的所有路径映射到单个控制器方法,但您必须使用HttpServletRequest而不是@PathVariable

因此,在您的情况下,它将是这样的:

@PostMapping("/view/**")
public ModelAndView view(HttpServletRequest request) {
    String pathVariable = extractId(request);         

    ModelAndView modelAndView = new ModelAndView();
    return modelAndView;
}

private String extractId(HttpServletRequest request) {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
}
另外,请查看此

查看此

其思想是通过添加**将以/view开头的所有路径映射到单个控制器方法,但您必须使用HttpServletRequest而不是@PathVariable

因此,在您的情况下,它将是这样的:

@PostMapping("/view/**")
public ModelAndView view(HttpServletRequest request) {
    String pathVariable = extractId(request);         

    ModelAndView modelAndView = new ModelAndView();
    return modelAndView;
}

private String extractId(HttpServletRequest request) {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
}

此外,请查看此

您可以使用前面共享的方法

@GetMapping(value = "blog/**")
public Blog show(HttpServletRequest request){   
    String id = (String) 
            request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    System.out.println(id);
    int blogId = Integer.parseInt(id);
    return blogMockedData.getBlogById(blogId);
}
第二种方法是使用RequestParam而不是Path变量

您将使用以下方法调用api:

http://localhost:8080/blog?input=nabcd/2/nr/dje/jfir/dye
控制器将如下所示: @GetMappingvalue=blog 公共博客show@RequestParaminput字符串输入{


如果您确定输入中的斜杠数,您可以使用此处提到的任何方法

您可以使用前面共享的方法

@GetMapping(value = "blog/**")
public Blog show(HttpServletRequest request){   
    String id = (String) 
            request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    System.out.println(id);
    int blogId = Integer.parseInt(id);
    return blogMockedData.getBlogById(blogId);
}
第二种方法是使用RequestParam而不是Path变量

您将使用以下方法调用api:

http://localhost:8080/blog?input=nabcd/2/nr/dje/jfir/dye
控制器将如下所示: @GetMappingvalue=blog 公共博客show@RequestParaminput字符串输入{

如果您确定输入中斜杠的数量,您可以使用这里提到的任何方法