2 Quick Start
Version: 3.1.0
2 Quick Start
Create The Application
Use Grails 3.x to create a new application. As an example, let's create an application called jaxrs-example:[budjb@laptop Projects]$ grails create-app jaxrs-example | Application created at /Users/budjb/Projects/jaxrs-example
Add JAX-RS Plugin to Dependencies
In thebuild.gradle
file, add the plugin to the project's dependencies. For the example,
we'll use the Jersey 1.x implementation.dependencies {
compile "org.grails.plugins:jaxrs-jersey1:1.0.0" // other dependencies below…
}
Be sure to use the updated version number of the plugin you are using. The version number above is likely out of date.
Create a Resource
The plugin comes bundled with a helper script to generate a basic resource, which we'll use here.[budjb@laptop jaxrs-example]$ grails create-resource com.budjb.TestBUILD SUCCESSFUL| Rendered template Resource.groovy to destination grails-app/resources/com/budjb/TestResource.groovy
package com.budjbimport javax.ws.rs.GET import javax.ws.rs.Path import javax.ws.rs.Produces@Path('/api/test') class TestResource { @GET @Produces('text/plain') String getTestRepresentation() { 'Test' } }
Start the Application
Everything we need is in place, so now we can start the application.[budjb@laptop jaxrs-example]$ ./gradlew bootRun Grails application running at http://localhost:8080 in environment: development > Building 97% > :jaxrs-example:bootRun
Try It Out!
Point your web browser tohttp://localhost:8080/api/test
, and you should be greeted with
the word Test
.