在Spring Application中拥有RestController和Controller的最佳实践 [英] Best practice to have a RestController and a Controller in Spring Application

查看:206
本文介绍了在Spring Application中拥有RestController和Controller的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是问这是否可能,我知道,但是我想知道在应用程序中拥有前端的同时提供休息服务的最佳方法是什么.

I am not asking if it is possible, I know it is but I would like to know what's the best way to offer a rest service while having a front end in my application.

我正在开发Spring Boot应用程序,当前有一个调用jsp页面的控制器和一个单独 RestController.我希望能够在Android应用程序中使用它.

I'm developing a Spring Boot application, I currently have a controller that calls jsp pages, and a separate RestController. I want to be able to consume it with an Android application.

那么在我的应用程序中同时包含Controller和单独 Restcontroller是否正确?例如,将从/api/*调用Rest控制器方法.

So is it correct to have both a Controller and a separate Restcontroller in my application? For example the Rest controller methods will be called from /api/*.

我知道两者之间的区别,但是由于我希望能够返回一个视图(并且我不应该使用RestController来做到这一点)并且我想要一个rest服务,所以我想知道是否可以同时拥有它们(当然是分开的.

Edit : I know the difference between the two, but since I want to be able to return a view ( and I shouldn't do that with a RestController) and I want to have a rest service I am wondering if I can have both of them (separately of course).

非常感谢您.

推荐答案

我会说这是可能的,但在典型的分层结构中,这是一种不好的做法(除了诸如REST应用程序中Swagger的控制器之类的极端情况) ,春季版应用程序(将返回到该版本)
您可能要创建一个多模块项目,如下所示:

I'd say that it's possible, but considered a bad practice (except for corner cases like a controller for Swagger in a REST application) in a typical, layered, spring app (will get back to that)
you may want to create a multi-module project, that may look like that:

  • 父项目
    -核心
    -Web API(基于jsp)
    -rest api(适用于android)
  • parent project
    -- core
    -- web api (jsp based)
    -- rest api (for android)

Web api和rest api都取决于核心.
Web api和rest api是单独的部署单元.您可以将它们部署在同一服务器上,也可以作为单独的应用程序运行(例如,使用spring boot).取决于您的用例.
这样一来,您就可以将业务逻辑放在一个地方(核心).

both web api and rest api depend on the core.
web api and rest api are separate deployment units. you can deploy them on the same server or run as separate applications (for example using spring boot). Depends on your usecase.
with that you can have your business logic in one place (core).

您可能还想了解ports and adapters架构,这可能使您有一个思路,不仅是将Controllers和RestControllers并排坐着,还应该以一种更有条理的方式解决该问题

you may also want to read about ports and adapters architecture, which may give you an idea how to solve this in a more organized way than just having Controllers and RestControllers sitting side by side

这篇关于在Spring Application中拥有RestController和Controller的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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