使用Spring 3.0,我可以有一个可选的路径变量吗?
例如
@RequestMapping(value = "/json/{type}", method = RequestMethod.GET)
public @ResponseBody TestBean testAjax(
HttpServletRequest req,
@PathVariable String type,
@RequestParam("track") String track) {
return new TestBean();
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想/json/abc或/json称为相同的方法.
一个明显的解决方法是声明type为请求参数:
@RequestMapping(value = "/json", method = RequestMethod.GET)
public @ResponseBody TestBean testAjax(
HttpServletRequest req,
@RequestParam(value = "type", required = false) String type,
@RequestParam("track") String track) {
return new TestBean();
}
Run Code Online (Sandbox Code Playgroud)
然后/json?type=abc&track=aa或/json?track=rr将工作
ear*_*las 183
您不能拥有可选的路径变量,但您可以使用两个调用相同服务代码的控制器方法:
@RequestMapping(value = "/json/{type}", method = RequestMethod.GET)
public @ResponseBody TestBean typedTestBean(
HttpServletRequest req,
@PathVariable String type,
@RequestParam("track") String track) {
return getTestBean(type);
}
@RequestMapping(value = "/json", method = RequestMethod.GET)
public @ResponseBody TestBean testBean(
HttpServletRequest req,
@RequestParam("track") String track) {
return getTestBean();
}
Run Code Online (Sandbox Code Playgroud)
Ani*_*kur 106
如果您在使用Spring 4.1和Java 8,你可以使用java.util.Optional它在支持@RequestParam,@PathVariable,@RequestHeader和@MatrixVariableSpring MVC中-
@RequestMapping(value = {"/json/{type}", "/json" }, method = RequestMethod.GET)
public @ResponseBody TestBean typedTestBean(
@PathVariable Optional<String> type,
@RequestParam("track") String track) {
if (type.isPresent()) {
//type.get() will return type value
//corresponds to path "/json/{type}"
} else {
//corresponds to path "/json"
}
}
Run Code Online (Sandbox Code Playgroud)
小智 74
众所周知,您还可以使用@PathVariable注释注入路径变量的Map.我不确定这个功能是否在Spring 3.0中可用或者是否稍后添加,但这是解决该示例的另一种方法:
@RequestMapping(value={ "/json/{type}", "/json" }, method=RequestMethod.GET)
public @ResponseBody TestBean typedTestBean(
@PathVariable Map<String, String> pathVariables,
@RequestParam("track") String track) {
if (pathVariables.containsKey("type")) {
return new TestBean(pathVariables.get("type"));
} else {
return new TestBean();
}
}
Run Code Online (Sandbox Code Playgroud)
Mal*_*k13 23
你可以使用:
@RequestParam(value="somvalue",required=false)
Run Code Online (Sandbox Code Playgroud)
对于可选参数而不是pathVariable
@GetMapping({"/dto-blocking/{type}", "/dto-blocking"})
public ResponseEntity<Dto> getDtoBlocking(
@PathVariable(name = "type", required = false) String type) {
if (StringUtils.isEmpty(type)) {
type = "default";
}
return ResponseEntity.ok().body(dtoBlockingRepo.findByType(type));
}
Run Code Online (Sandbox Code Playgroud)
@GetMapping({"/dto-reactive/{type}", "/dto-reactive"})
public Mono<ResponseEntity<Dto>> getDtoReactive(
@PathVariable(name = "type", required = false) String type) {
if (StringUtils.isEmpty(type)) {
type = "default";
}
return dtoReactiveRepo.findByType(type).map(dto -> ResponseEntity.ok().body(dto));
}
Run Code Online (Sandbox Code Playgroud)
Nicolai Ehmann 的评论和 wildloop 的答案的简化示例(适用于 Spring 4.3.3+),基本上您required = false现在可以使用:
@RequestMapping(value = {"/json/{type}", "/json" }, method = RequestMethod.GET)
public @ResponseBody TestBean testAjax(@PathVariable(required = false) String type) {
if (type != null) {
// ...
}
return new TestBean();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
142890 次 |
| 最近记录: |