简体   繁体   English

是否为REST API使用单独的控制器?

[英]Advice on using separate controllers for a REST API or not?

we are planning a REST api for our application and are trying to make a decision on if we should implement separate controllers for the REST functionality or not. 我们正在为我们的应用程序规划一个REST api,并且正在尝试决定是否应该为REST功能实现单独的控制器。

We could use the withFormat{} in our current controllers but separating the REST functionality in different controllers feels somewhat cleaner.. 我们可以在当前控制器中使用withFormat {},但将不同控制器中的REST功能分开感觉更清晰一些。

In that way we can build our API seperate from the current controllers and we could even take the REST controllers into another application etc. 通过这种方式,我们可以从当前控制器中单独构建API,甚至可以将REST控制器转换为其他应用程序等。

Any thoughts on this subject? 有关这个问题的任何想法? Any real world experience in what the best practice would be? 什么是最佳实践的真实世界经验?

We recently faced the same decision and we decided to go for separate controllers for the REST API. 我们最近面临同样的决定,我们决定为REST API寻找单独的控制器。

Advantages of separate controllers include cleaner/clearer controller actions and the possibility to support different versions of the REST API later on. 独立控制器的优点包括更清晰/更清晰的控制器操作以及稍后支持不同版本的REST API的可能性。

We also would like to keep the option to host the REST API on separate server instances open. 我们还希望保留在打开的单独服务器实例上托管REST API的选项。 These servers would use exactly the same .war, but with different configuration settings for the feature toggles . 这些服务器将使用完全相同的.war,但具有不同的配置设置以切换功能

A disadvantage of separate controllers might be the DRYness of the controller code. 单独控制器的缺点可能是控制器代码的干燥。 Although this should be limited, since IMHO you should keep the controllers as thin as possible and extract shared logic to Grails services or helper classes. 虽然这应该是有限的,因为恕我直言,你应该尽可能保持控制器的精简,并提取共享逻辑到Grails服务或助手类。

I will work with grails soon, but so far i have little experience with it. 我很快就会用grails工作,但到目前为止我对它几乎没有经验。 But in web apps i worked, we always left webservices separated from the controller code. 但在我工作的网络应用程序中,我们总是将webservices与控制器代码分开。 We also separated REST from SOAP. 我们还将REST与SOAP分开。 Common methods for them would be in service layer. 它们的常用方法是服务层。 It, indeed, felt cleaner. 事实上,它感觉更清洁。 We didn't had to insert a lot of if s in the methods 我们没有必要在方法中插入很多if

I would, for a given resource, use one controller that interfaces with a service layer based on context (the media type received or requested -- SOAP, JSON, XML, etc.) This way, you have a truly uniform resource identifier that can accept and return various media types and the controller won't need to know anything but what method the user wants to perform on what resource and what media type is involved. 对于给定的资源,我会使用一个控制器,该控制器基于上下文(接收或请求的媒体类型 - SOAP,JSON,XML等)与服务层接口。这样,您就拥有了一个真正统一的资源标识符 ,可以接受并返回各种媒体类型,控制器不需要知道任何东西,除了用户想要在什么资源和涉及的媒体类型上执行什么方法。

For instance, maybe the service layer returns objects that have methods such as 'toXml', 'toSoap', or 'toJson'. 例如,服务层可能返回具有诸如'toXml','toSoap'或'toJson'之类的方法的对象。 Then you can just ask the service layer to do whatever and use a switch statement on the requested media type to either return the requested information, or by default throw a 406 Not Acceptable status code. 然后,您可以要求服务层执行任何操作,并在请求的媒体类型上使用switch语句来返回所请求的信息,或者默认情况下抛出406 Not Acceptable状态代码。 For unsafe or idempotent transactions, the object may have constructor or factory methods for a given media type and then you just ask the service layer to do whatever with that object. 对于不安全或幂等事务,对象可能具有给定媒体类型的构造函数或工厂方法,然后您只需要服务层对该对象执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM