我应该使用 Spring restful 网络服务还是控制器来生成 json?

Should I use Spring restful web service or controllers to generate json?

我已经使用 Spring 4 构建了一个网络应用程序。该应用程序允许搜索用户、创建元素等。

要求发生了变化。现在我必须将搜索结果作为 json 而不是 html(但保留 html 以防万一)。

问题是,我应该使用控制器并根据参数重定向到带有 json/html 的 jsp 吗?或者使用 Spring restful 网络服务?(我从来没有用过这个,需要学习如何使用)如果我使用 Spring restful 我必须做同样的工作吗我已经用网页版生成 json?

这取决于您之前的 MVC 应用程序是如何设计的。

我的建议如下:

但最好使用 Spring REST 控制器。机制或多或少与 MVC 相同。我假设您的应用程序的 CRUD 数据有 service 层。

最好使用 ServiceRepository 并为您的应用程序创建 @RestController@ResponseBody 应该是您从控制器返回的模型。 @RequestBody 将保存一般 POST, PATCH, DELETE 请求的数据。

祝你好运。