Grails service methods to expose as REST services - grails

I am new to Grails. I have seen how to expose the controller actions/methods as RESTFull services using UrlMapping.groovy file and using GET/POST/PUT/DELETE http methods.
Now following is my questions.
1.Is there any way exposing the service class methods (/service folder in Grails apps) as RESTFull services and access them in similar way as i accessed the controller actions as described above.
2.Is there a way to expose the service class methods as SOAP services?

You would like to look at JAXRS plugin and CXF plugin and this page for reference, if you have not transitioned to Grails 2.3 yet.
Grails 2.3.0 comes with a renaissance of REST services.
Note
JAXRS plugin will help in achieving what you needed from question # 1 agreeing to the comment from #TP_JAVA about exposing service methods as controller action.

Related

Grails 3 change default service scope

In grails 3, the default service scope is Singleton, the documents show it's easy to override this by defining
static scope='request'
in the service class. Is it possible to change the default service scope for an application similar to the way it is done for controllers in application.groovy?
The specific issue is a Service class in a plugin is calling application services (which are designed around request scope). This was working in grails 2, but with the upgrade to grails 3 it no longer does.
Is it possible to change the default scope for an application similar
to the way it is done for controllers in application.groovy?
There is no direct support for that, no. You could write a bean definition post processor that could impose that change.

How to Access OWIN Connector after Startup in MVC

I have a MVC project that uses OwinConnectorFactory and ConnectorBuilder in startup.cs to create a connector, using the passed-in IAppBuilder. How do I access the connector after startup? Let's say, during authentication?
I've tried the http owin context but couldn't find connectors in request/response items. I didn't know where else to look.
TIA
One approach how to do it is via IoC, if you are using it in the project. Initialize you container right in the class marked by OwinStarutup attribute where all middle layers are wired up too.
Add these instances into the container and leave DI to inject them where ever required.

How I can make a webservice to be published in multiple addresses (endpoints) using CXF with Grails?

I am implementing a web Service in grails using CXF. I want to publish my endpoint in several addresses for a single Service. Like as:
localhost:8080/myapplication/Services/myservice1
localhost:8080/myapplication/Services/myservice2
And how can acheive that endpoint addresses dynamically?
Thank you.
Try CXF plugin for grails. At the end of readme there is even a demo project.

How to implement a "remote" Domain?

Imagine two Grails applications which share a domain class. Maybe a Book domain class.
One application is identified as the owner of the data, one will have to access the domain data. Something like amazon and the amazon web services.
I guess it is trivial that the owning application will use a normal domain class and will expose the data through web services - no problem in grails.
But what would be best practice to implement the domain in the other application?
use a service to access the remote domain and not implement a local domain class at all?
implement a local domain class, overwrite the get()-method in order to fetch the remote data and use the local database as cache?
what other solution comes to your mind?
Ryan Geyer has a very interesting article Modularizing your Grails application domain classes which lists 3 solutions to this problem:
As a RESTful JSON Service - easy to get this setup in Grails but then you lose the automatic GORM functionality.
Separate out the domain classes into a library JAR file and reference that library in both of my other applications. This is not as easy as it first sounds
Create a Grails plugin. Put the domain object in the plugin. Each of your applications can then import this plugin. You can then create different controllers with different functionality as required. The sample code for this is available at:
git clone git://ec2.nslms.com/grails/blog_example_modular
Ted Naleid gives a great tip later in the post and recommends...
"create a grails-app/conf/BuildConfig.groovy file and put the plugin name and path to the source in it. ... If you do this, your applications will see the live changes to your domain/controller/service/etc classes as if they were actually in current app and there isn't any need to repackage and reinstall the plugin when you make changes."
Using memcache should enable both applications to have a consistent view of the data and would avoid each individual application having it's own inconsistent cache.
I think you can make JAR file of your domain classes and add reference to other grails application .
Found another interesting solution:
Riak is a key/value database with a first class REST API. There is a grails plugin for riak which maps most of the GORM functionality (relations, dynamic finders etc) to the REST API of riak: http://grails.org/plugin/riak
Now comes the part which I haven't tested yet: if you make use of the DataSources feature of grails 2.0, it should be possible to only connect those "remote" domains to a riak database.
As a result, there would be a domain stored in a riak database and several applications would be able to access it via a clean REST API without effort.
OK. This also show how silly my question is - it would be the same if you connect several apps through the same SQL-database. But sometimes people want to have something more funky like webservices.

Using Grails without an user interface

I'm thinking about possible alternatives for our EJB based service layer and wondered if it would make sense to use just the service and database layer of Grails together with the Remoting Plugin or is this using a sledgehammer to crack a nut?
Speaking of the Remoting Plugin: is there a standard way of generating a JAR file, that contains the necessary classes to make a remote call to a Grails service from a non-Spring Java application?
Interesting idea. I don't think it'd be overkill at all. The nice thing is that your service would be very portable across protocols and deployment options (e.g. put a controller layer on top and it's instantly embedded). This gives you the benefits of EJB's (persistence) + the ability to use Groovy and GORM.
FWIW, we're using Grails as our service tier; in come cases we use it embedded (as a plugin), in others we expose the services (via controllers) as JSON or SOAP; I see exposing as RMI as a variation of what we're doing (without the controller layer).

Resources