RestController中想用普通的控制器怎么办
时间: 2023-10-19 07:28:55 浏览: 29
如果你想在一个 `RestController` 中使用普通的控制器,你可以通过注入 `Controller` 类来实现。首先,确保你的普通控制器类被标记为 `@Controller`,并在类中定义适当的请求映射和处理方法。
然后,在你的 `RestController` 类中,使用 `@Autowired` 注解将普通控制器类注入进来。这样你就可以在 `RestController` 类中调用普通控制器的方法了。
下面是一个示例代码:
```java
@Controller
public class MyController {
@RequestMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
@RestController
public class MyRestController {
@Autowired
private MyController myController;
@RequestMapping("/api/hello")
public String hello() {
return myController.hello();
}
}
```
在上面的示例中,`MyController` 是一个普通的控制器类,其中定义了一个 `/hello` 的请求映射方法。然后,我们将 `MyController` 注入到 `MyRestController` 中,并在 `/api/hello` 的请求映射方法中调用了 `myController.hello()` 方法。
希望这个示例对你有所帮助!如果你还有其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)