Spring中用于单个请求Map的多个Controller类

gopyfrb3  于 2022-10-30  发布在  Spring
关注(0)|答案(2)|浏览(163)

我的控制器类有更多的rest方法,所以我想将我的控制器类拆分为多个子类,但我希望所有类都有相同的请求Map
范例

@RequestMapping("/Student")
public class StudentController {
@PostMapping(consumes = "application/json", produces = "application/json")
    public ResponseEntity<Object> saveStudnt(HttpServletRequest request)
{

}
@GetMapping(consumes = "application/json", produces = "application/json")
    public ResponseEntity<Object> getStudent(HttpServletRequest request)
{

}
}

我想将这两个方法分离到两个单独的控制器中,但我希望使用相同的请求Map/学生访问
这在春 Boot 中可能吗?

egmofgnx

egmofgnx1#

为什么不呢?在运行时,Spring只考虑最终端点,而不依赖于它们的位置。你可以在不同的类中拥有相同的方法名,但是一个端点只有一个路径。

hgqdbh6s

hgqdbh6s2#

您可以将方法分为两个类,但据我所知,这不是最佳实践

public class ControllerPost {
@PostMapping(path ="/Student" consumes = "application/json", produces = "application/json")
    public ResponseEntity<Object> saveStudnt(HttpServletRequest request)
{
}
}

public class ControllerGet {
@GetMapping(path ="/Student" consumes = "application/json", produces = "application/json")
    public ResponseEntity<Object> getStudent(HttpServletRequest request)
{
}
}

相关问题