是否可以在两种不同的 post 方法的请求映射中映射相同的路径(uri),唯一的区别是请求正文。
示例
@RequestMapping(value = "/hello", method = RequestMethod.POST)
public String helloEmployee(@RequestBody Employee employee) {
return "Hello Employee";
}
@RequestMapping(value = "/hello", method = RequestMethod.POST)
public String helloStudent(@RequestBody Student student) {
return "Hello Student";
}
最佳答案
不,您不能在具有不同请求正文类型但相同媒体类型的 post 方法的请求映射中给出相同的 url。以下行不通:
@PostMapping(path = "/hello", consumes = MediaType.APPLICATION_JSON_VALUE)
public String hello(@RequestBody Pojo1 val) {
return "Hello";
}
@PostMapping(path = "/hello", consumes = MediaType.APPLICATION_JSON_VALUE)
public String hello(@RequestBody Pojo2 val) {
return "Hello";
}
如果您有不同的媒体类型,那么它就会。下面将起作用:
@PostMapping(path = "/hello", consumes = MediaType.APPLICATION_JSON_VALUE)
public String hello(@RequestBody Pojo val) {
return "Hello";
}
@PostMapping(path = "/hello", consumes = MediaType.TEXT_PLAIN_VALUE)
public String hello(@RequestBody String val) {
return "Hello";
}
您的 RequestMapping
应至少在一个条件上有所不同; 路径、方法、参数、 header 、消耗、产生
关于java - Spring - 是否可以在请求映射中为两种不同的 post 方法提供相同的 url(路径)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53519006/