Java 如果@PathVariable为';什么东西没找到?
如果路径变量不在url中,是否可以使Java 如果@PathVariable为';什么东西没找到?,java,spring-3,spring-annotations,path-variables,Java,Spring 3,Spring Annotations,Path Variables,如果路径变量不在url中,是否可以使@PathVariable返回null?否则我需要做两个处理器。一个用于/simple,另一个用于/simple/{game},但如果没有定义游戏,我会从列表中选择第一个,但是如果定义了游戏参数,我会使用它 @RequestMapping(value = {"/simple", "/simple/{game}"}, method = RequestMethod.GET) public ModelAndView gameHandler(@PathVariable
@PathVariable
返回null?否则我需要做两个处理器。一个用于/simple
,另一个用于/simple/{game}
,但如果没有定义游戏,我会从列表中选择第一个,但是如果定义了游戏参数,我会使用它
@RequestMapping(value = {"/simple", "/simple/{game}"}, method = RequestMethod.GET)
public ModelAndView gameHandler(@PathVariable("example") String example,
HttpServletRequest request) {
这就是我在尝试打开页面/simple
时得到的结果:
原因:java.lang.IllegalStateException:在@RequestMapping中找不到@PathVariable[示例]
它们不能是可选的,不。如果需要,需要两种方法来处理它们
这反映了路径变量的性质——它们为空实际上没有意义。REST样式的URL始终需要完整的URL路径。如果您有一个可选组件,请考虑将其作为请求参数(即使用<代码> @ RequestParam >代码>)。这更适合于可选参数。您始终可以这样做:
@RequestMapping(value = "/simple", method = RequestMethod.GET)
public ModelAndView gameHandler(HttpServletRequest request) {
gameHandler2(null, request)
}
@RequestMapping(value = "/simple/{game}", method = RequestMethod.GET)
public ModelAndView gameHandler2(@PathVariable("game") String game,
HttpServletRequest request) {
正如其他人已经提到的No一样,当您明确提到路径参数时,您不能期望它们为null。但是,您可以执行以下操作作为解决方法-
@RequestMapping(value = {"/simple", "/simple/{game}"}, method = RequestMethod.GET)
public ModelAndView gameHandler(@PathVariable Map<String, String> pathVariablesMap,
HttpServletRequest request) {
if (pathVariablesMap.containsKey("game")) {
//corresponds to path "/simple/{game}"
} else {
//corresponds to path "/simple"
}
}
尝试这种方法,它对我很有效。我刚刚测试过,但通过结合上述解决方案,我得到了以下结果:
@RequestMapping(value = {"/simple", "/simple/{game}"}, method = RequestMethod.GET)
public ModelAndView gameHandler(@PathVariable(value = "game", required = false) String example,
HttpServletRequest request) {
if (example != null) {
//...
} else {
//pick first, ...
}
}
现在,当您使用“/simple”时,字符串示例将为null,而不是引发异常
简短的解决方案,没有花哨的可选或映射我们可以在控制器中编写多个方法,使用路径变量组合进行显式映射,以排除可选变量(如果使用旧版本的Spring) 在我的场景中,我想开发一个API来获取旧设备的回收价值,其中的参数可以是品牌、型号和网络,但网络是一个选项 处理此问题的一个选项是使用网络作为请求参数,而不是pathVariable。 例如/value/LG/g3?network=沃达丰,但我不喜欢这种方法 对我来说,下面用的是更干净的 /加油值/LG/g3 /refurbValue/LG/g3/沃达丰
@RequestMapping(value = "/refurbValue/{make}/{model}/{network}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
def getRefurbValueByMakeAndModelAndNetwork(@PathVariable String make, @PathVariable String model, @PathVariable String network ) throws Exception {
//logic here
}
@RequestMapping(value = "/refurbValue/{make}/{model}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
def getRefurbValueByMakeAndModel(@PathVariable String make, @PathVariable String model) throws Exception {
//logic here
}
在上面的示例中,两个控制器可以使用相同的服务方法,并且可以处理参数。在我的例子中,我使用的是Groovy,所以它很容易与可选参数一起使用,比如
Map getRefurbValue(字符串品牌,字符串模型,字符串网络=”)但是,sine v4.3.3 Spring支持
@PathVariable(value=“siteId”,required=false)String siteId
尽管我没有找到一种方法不提供路径变量或将其保留为空或空白。@Alex尽管可以将必需的参数设置为false
,但仍然需要提供路径参数,否则将引发异常。
@RequestMapping(value = {"/simple", "/simple/{game}"}, method = RequestMethod.GET)
public ModelAndView gameHandler(@PathVariable(value = "game", required = false) String example,
HttpServletRequest request) {
if (example != null) {
//...
} else {
//pick first, ...
}
}
@RequestMapping(value = "/refurbValue/{make}/{model}/{network}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
def getRefurbValueByMakeAndModelAndNetwork(@PathVariable String make, @PathVariable String model, @PathVariable String network ) throws Exception {
//logic here
}
@RequestMapping(value = "/refurbValue/{make}/{model}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
def getRefurbValueByMakeAndModel(@PathVariable String make, @PathVariable String model) throws Exception {
//logic here
}