Vaadin 7 on Grails 2.x
  • Introduction
  • Project setup
    • Command Line
    • IntelliJ IDEA
    • Eclipse
    • NetBeans
    • Plugin Configuration
    • Clean up
    • Best Practices
  • Database
    • GORM
      • Create Domain Model
      • Transactions
      • LazyInitializationException
      • Open Session In View I.
      • Open Session In View II.
      • Table Container
    • Groovy SQL
      • Create Sql
      • Execute SQLs
    • MyBatis
      • Configuration
      • Basics
    • JdbcTemplate
      • Create Beans
      • Usage
    • Clean Up With Alternatives
  • Architecture
    • Model View Presenter
  • Spring Autowiring
    • Create Simple Application
    • Application With Navigator
  • UI
    • Re-using GORM Validations
    • Async Push
    • Multiple application
    • SASS Compilation
    • Widgetset Compilation
  • Spring Security
    • Spring Security Dependency
    • Spring Security Basics
    • Secured Navigator
  • Localization
    • Localization Basics
    • Custom MessageSource
  • REST
    • Without using root URL
    • Using root URL for Vaadin app
  • Plugin development
    • Github
    • Development
Powered by GitBook
On this page
  • Step 1
  • Step 2
  • Step 3
  • Step 4
  • Step 5

Was this helpful?

  1. REST

Without using root URL

PreviousRESTNextUsing root URL for Vaadin app

Last updated 5 years ago

Was this helpful?

Example code is available on .

If you need to provide REST API from your application and you do not want to provide that from other application, there is a way how to do it from Grails even if we run there Vaadin applications.

First thing if you want to have REST API, do not map any Vaadin UI on /* (root) url. If you have to use the root, check the next article "Using root URL for Vaadin app".

Step 1

Your mapping should like like this.

mapping = [
    "/admin/*": "app.AdminUI",
    "/client/*": "app.ClientUI"
]

Step 2

Then, we can add URL mapping into UrlMappings.groovy.

class UrlMappings {
    static mappings = {
        group "/rest", {
            "/$controller/$action?/$id?"()
        }
    }
}

Step 3

Create a controller grails create-controller app.ItemController and implement some logic there. For example, fetch some data from database and return it as JSON. In our example, we will just return a map, with dummy data, as JSON.

package app

import grails.converters.JSON

class ItemController {

    def index() {
        Map data = ['some': 'data']

        return data as JSON
    }
}

Step 4

Create two sample Vaadin UIs, so we can verify URL mapping.

package app

import com.vaadin.grails.ui.DefaultUI
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.Label
import com.vaadin.ui.VerticalLayout

class ClientUI extends DefaultUI {

    protected void init(VaadinRequest request) {
        VerticalLayout layout = new VerticalLayout()
        layout.setMargin(true)

        Label label = new Label("Client")
        layout.addComponent(label)

        setContent(layout)
    }
}
package app

import com.vaadin.grails.ui.DefaultUI
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.Label
import com.vaadin.ui.VerticalLayout

class AdminUI extends DefaultUI {

    protected void init(VaadinRequest request) {
        VerticalLayout layout = new VerticalLayout()
        layout.setMargin(true)

        Label label = new Label("Admin")
        layout.addComponent(label)

        setContent(layout)
    }
}

Step 5

Start up the application and try out different URLs:

github.com/vaadin-on-grails/rest-without-root
http://localhost:8080/rest-without-root/client
http://localhost:8080/rest-without-root/admin
http://localhost:8080/rest-without-root/rest/item/index