Step by Step Tutorial Grails 3 REST Web Service: Part 2

by Didin J. on Feb 09, 2017 Step by Step Tutorial Grails 3 REST Web Service: Part 2

More deeply with Grails 3 RESTful Web Service with another way to create RESTful Web Service using Grails 3 URL Mappings.

Previously, we have created basic Grails 3 REST web service in domain or entity. Now, continue with another way to create Grails 3 RESTful Web Service using built-in Grails 3 features also using classic way of RESTful API creation. Let's make it short with tutorials.


1. Create Grails 3 REST Web Service from URL Mapping

Open last created project in part 1 using your text editor or IDE. To using URL Mapping for creating Grails 3 REST Web Service, firstly generate the controller for existing domain class. Open terminal or cmd and go to project folder then run Grails interactive console.

grails

Inside Grails interactive console run this command for generate, controller.

generate-controller com.djamware.Article

It will show output like this.

| Rendered template Controller.groovy to destination grails-app/controllers/com/djamware/ArticleController.groovy
| Rendered template Spec.groovy to destination src/test/groovy/com/djamware/ArticleControllerSpec.groovy
| Scaffolding completed for grails-app/domain/com/djamware/Article.groovy

Now remove "@Resource(uri='/..')" from domain or entity class. Then open and edit "grails-app/controllers/package-name/UrlMappings.groovy" then add this lines.

"/article"(resources:"article")

Now, just run your app.

run-app


2. Accessing REST API from Terminal

To test this new configuration, open another terminal or cmd then type this command to get data from the server.

curl -i -H "Accept: application/json" localhost:8080/article/1

The output should be like this.

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Application-Context: application:development
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 08 Feb 2017 01:36:10 GMT

{"id":1,"author":"Didin J.","category":{"id":1},"content":"Step by step tutorial on how to create Grails web application from scratch","createDate":"2017-02-08T01:33:59Z","description":"Step by step tutorial on how to create Grails web application from scratch","title":"How to Create Grails Web Application"}

To create new data, use this command. Because generated controller use default response as web page, add ".json" after endpoint.

curl -i -X POST -H "Content-Type: application/json" -d '{"category":{"id":1},"title":"Article Title 3","author":"Didin J.","description":"Article Description 3","content":"Article Content 3"}' localhost:8080/article.json

You will see a result like this if succeed.

HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
X-Application-Context: application:development
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 08 Feb 2017 15:26:45 GMT

{"id":3,"author":"Didin J.","category":{"id":1},"content":"Article Content 3","createDate":"2017-02-08T15:26:45Z","description":"Article Description 3","title":"Article Title 3"}

To edit data, use this command.

curl -i -X PUT -H "Content-Type: application/json" -d '{"title":"Programming Cobol"}' localhost:8080/article/2.json

If you see the response like below, data is updated.

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Application-Context: application:development
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 08 Feb 2017 15:30:09 GMT

{"id":2,"author":"Didin J.","category":{"id":1},"content":"Article Content 2","createDate":"2017-02-08T15:23:52Z","description":"Article Description 2","title":"Programming Cobol"}

To delete data, simply use this command.

curl -i -X DELETE localhost:8080/article/2.json

The response just like this.

HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
X-Application-Context: application:development
Content-Type: application/json;charset=UTF-8
Date: Wed, 08 Feb 2017 15:31:12 GMT


That it's for now, next we have will continue with Controller implementation.

Thanks.

Loading…