一个带有两个控制器的实体类(@RestController 和 @Controller) [英] One entity class with two controllers (@RestController and @Controller)

查看:36
本文介绍了一个带有两个控制器的实体类(@RestController 和 @Controller)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个同时处理 @RestController@Controller 的 Web 应用程序.据我了解, @RestController 用于 API 测试,使用 postman 处理 crud 进程,另一方面 @Controller 用于处理模型视图中的 crud.是否可以在 Spring boot 应用程序中为一个实体类创建这两个控制器,并使它们都从处理 crud 实现的逻辑的服务类调用相同的方法?请提供一个示例来解释您的实体类和两个控制器,以便为我清除想法.

I am trying to build a web application that handles both @RestController and @Controller. As far as I understand that @RestController is for API testing using postman to handle crud processes on the other hand @Controller is to handle crud in model view. Is it possible to create both of these controllers for one entity class in Spring boot application and make them both call the same method from the service class which handles the logic of the crud implementation? Please provide an example to your explanation with an entity class and both of the controllers to clear the idea for me.

推荐答案

  1. RestController 是 Controller 和 ResponseBody 注解的元注解.注释是从 Spring 4.0 版本购买的,它可以帮助您设计控制器,其响应不是 ModelandView,而是返回值是 JSON/XML 形式的原始数据(还有其他类型的 MediaType,可以使用生产和消耗@RequestMapping的参数)
  2. 当您定义控制器时,调度程序 servlet 与 ViewResolver 对话以将返回的字符串解析为视图/页面.在@RestController 的情况下,调度程序 servlet 使用 HttpMessageConverters 以请求格式 - json/xml 将原始响应发送回客户端.
  3. 因此 REST API 是一个端点,可帮助您通过网络公开您的资源以供另一个 SERVICE 使用;Spring 帮助您使用 @RestController 类编写 REST API.如果您使用 jsp/thymeleaf 作为与应用程序进行人机交互的渲染引擎来处理基于 Web 的应用程序,您将使用 @Controller.

所以,你写了什么:

@RestContrller 用于 API 测试,使用 postman 处理 crud另一方面,进程@Controller 是处理模型中的 crud查看

@RestContrller is for API testing using postman to handle crud processes on the other hand @Controller is to handle crud in model view

并不完全正确.您使用 @RestController 构建 REST API.您使用 Postman 测试端点,但 @RestController 不用于使用 Postman 的 API 测试.您使用它来构建端点并使用 postman 对其进行测试(您也可以使用其他客户端和框架来测试您的端点).

is not entirely correct. You build REST API using @RestController. You test the endpoints using Postman, but @RestController is not used for API testing using postman. You use it to build the endpoint and test it with postman(you can use other clients and frameworks to test your endpoint too).

回答您的问题:

是否可以为一个实体创建这两个控制器Spring 启动应用程序中的类并使它们都调用相同的来自处理 crud 逻辑的服务类的方法实施?

Is it possible to create both of these controllers for one entity class in Spring boot application and make them both call the same method from the service class which handles the logic of the crud implementation?

是的,可以让@REstController 和@Controller 为同一个实体使用同一个服务类(事实上,如果您的视图和json 响应的业务逻辑相同,您将只有一个服务bean).很可能您的应用程序有一个使用 jsp 为人类用户构建的前端,并且还公开了与其他服务交互的端点;这通常与大公司相关,因为大公司的多个服务在功能和数据方面相互依赖.但是您需要确保您的端点没有冲突或相同.通常,如果我们有一个控制器映射为/books -> 在屏幕上显示书籍列表,我们将有一个类似的其余控制器映射 -/api/v1/books -> 返回书籍列表.

Yes, it is possible to have @REstController and @Controller working with same service class for the same entity (in fact if business logic is same for your view and response in json- you will just have one service bean). It is quite possible that your application has a front-end built using jsp for human users and also expose endpoints to interact with other services; this is generally relevant in big companies where multiple services rely on each other for functionality and data. But you need to make sure your endpoints are not colliding or the same. Normally if we have a controller mapping as /books -> displays a list of books on the screen, we will have a similar rest controller mapping - /api/v1/books -> to return a list of books.

这篇关于一个带有两个控制器的实体类(@RestController 和 @Controller)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆