Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
spring启动:请求映射_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

spring启动:请求映射

spring启动:请求映射,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,我有以下三种REST API方法: @RequestMapping(value = "/{name1}", method = RequestMethod.GET) public Object retrieve(@PathVariable String name1) throws UnsupportedEncodingException { return configService.getConfig("frontend", name1); } @RequestM

我有以下三种REST API方法:

@RequestMapping(value = "/{name1}", method = RequestMethod.GET)
    public Object retrieve(@PathVariable String name1) throws UnsupportedEncodingException {
        return configService.getConfig("frontend", name1);
    }

@RequestMapping(value = "/{name1}/{name2}", method = RequestMethod.GET)
public Object retrieve(@PathVariable String name1, @PathVariable String name2) throws UnsupportedEncodingException {
    return configService.getConfig("frontend", name1, name2);
}

@RequestMapping(value = "/{name1}/{name2}/{name3}", method = RequestMethod.GET)
public Object retrieve(@PathVariable String name1, @PathVariable String name2, @PathVariable String name3) {
    return configService.getConfig("frontend", name1, name2,name3);
}
getConfig方法配置为接受多个参数,如:

 public Object getConfig(String... names) {
我的问题是:是否可以仅使用一个方法/RequestMapping实现上述RequestMapping


谢谢。

您可以使用
request.getAttribute(HandlerMapping.path\u在\u HANDLER\u MAPPING\u属性中)
检索完整路径,然后解析它以获取所有值。

您可能应该使用@RequestParam和方法POST来实现您想要的结果

@RequestMapping(name = "/hi", method = RequestMethod.POST)
@ResponseBody
public String test(@RequestParam("test") String[] test){

    return "result";
}
然后你就这样发帖:

因此,字符串数组将同时包含这两个值


同样在REST中,路径对应于一个资源,所以您应该问自己“我正在公开的资源是什么?”。它可能类似于/config/frontend,然后通过请求参数和/或HTTP谓词指定选项

您可以在映射中使用
/**
获取任何URL,然后从映射路径提取所有参数。Spring有一个常量,允许您从HTTP请求获取路径。您只需删除映射中不必要的部分,并拆分其余部分即可获得参数列表

import org.springframework.web.servlet.HandlerMapping;

@RestController
@RequestMapping("/somePath")
public class SomeController {

    @RequestMapping(value = "/**", method = RequestMethod.GET)
    public Object retrieve(HttpServletRequest request) {
        String path = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
        String[] names = path.substring("/somePath/".length()).split("/");
        return configService.getConfig("frontend", names);
    }

}
http://yourapp.com/somePath?name=value1&name=value2
更好的方法

但是,路径变量应该用于标识应用程序中的资源,而不是作为给定资源的参数。在这种情况下,建议使用简单的请求参数

import org.springframework.web.servlet.HandlerMapping;

@RestController
@RequestMapping("/somePath")
public class SomeController {

    @RequestMapping(value = "/**", method = RequestMethod.GET)
    public Object retrieve(HttpServletRequest request) {
        String path = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
        String[] names = path.substring("/somePath/".length()).split("/");
        return configService.getConfig("frontend", names);
    }

}
http://yourapp.com/somePath?name=value1&name=value2
映射处理程序看起来更简单:

@RequestMapping(method = RequestMethod.GET)
public Object retrieve(@RequestParam("name") String[] names) {
    return configService.getConfig("frontend", names);
}
这应该起作用:

@SpringBootApplication
@Controller
public class DemoApplication {


public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}

@RequestMapping(value ={"/{name1}","/{name1}/{name2}","/{name1}/{name2}/{name3}"})
public @ResponseBody String testMethod(
        @PathVariable Map<String,String> pathvariables)
{
    return test(pathvariables.values().toArray(new String[0]));
}

private String test (String... args) {
    return Arrays.toString(args);
}
@springboot应用程序
@控制器
公共类演示应用程序{
公共静态void main(字符串[]args){
run(DemoApplication.class,args);
}
@请求映射(值={/{name1},“/{name1}/{name2},“/{name1}/{name2}/{name3}”)
public@ResponseBody字符串测试方法(
@路径变量映射路径变量)
{
返回测试(pathvariables.values().toArray(新字符串[0]);
}
专用字符串测试(字符串…参数){
返回数组.toString(args);
}

}

为了记录在案,这是一个Spring MVC问题。SpringBoot更像是对所有Spring内容的包装器自动配置