[英]How to overload mapping with same prefix uri to different methods in Spring?
I'm trying to map two different URI with the same prefix to two different handlers 我正在尝试将具有相同前缀的两个不同URI映射到两个不同的处理程序
POST "resource" -> resourcesBl.create POST“资源”-> resourcesBl.create
POST "resource/{path_variable}" -> resourcesBl.createOther POST“资源/ {path_variable}”-> resourcesBl.createOther
note - the request body for each one is different 注意-每个请求的主体都不同
and Spring doesn't do it automatically 而且Spring不会自动执行
@RestController
@RequestMapping("resource")
public class MyController {
@Autowired
private ResourcesBl resourcesBl;
@PostMapping
public Resource create(@RequestBody Resource resource) {
return resourcesBl.create(resource);
}
@PostMapping("/{resourceName}"}
public OtherResource create(@PathVariable String resourceName, @RequestBody OtherResource otherResource, ) {
return resourcesBl.createOther(otherResource);
}
}
I have tried this solution with Postman, it works perfectly fine and takes the path variable, both mappings can be called with different bodies. 我已经在Postman上尝试过此解决方案,它的工作原理非常完美,并且采用path变量,可以使用不同的主体调用这两个映射。
@RestController
@RequestMapping("/resource")
public class MyController {
@Autowired
private ResourcesBl resourcesBl;
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public Resource create(@RequestBody Resource resource) {
return resourcesBl.create(resource);
}
@RequestMapping(value = "/{resourceName}", method = RequestMethod.POST)
@ResponseBody
public OtherResource create(@PathVariable String resourceName,
@RequestBody OtherResource otherResource) {
return resourcesBl.createOther(resourceName, otherResource);
}
}
All requests with empty path params will be mapped to the first method, so both: "/resource" and "/resource/". 所有路径参数为空的请求都将映射到第一个方法,因此都为“ / resource”和“ / resource /”。 Anything after the slash to the second. 斜线之后的所有内容。
Note: don't forget to use the path variable. 注意:不要忘记使用path变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.