> For the complete documentation index, see [llms.txt](https://ondrej-kvasnovsky-2.gitbook.io/vaadin-on-grails/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ondrej-kvasnovsky-2.gitbook.io/vaadin-on-grails/rest/without_using_root.md).

# Without using root URL

> Example code is available on [github.com/vaadin-on-grails/rest-without-root](https://github.com/vaadin-on-grails/rest-without-root).

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.

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

### Step 2

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

```java
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.

```java
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.

```java
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)
    }
}
```

```java
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:

* <http://localhost:8080/rest-without-root/client>
* <http://localhost:8080/rest-without-root/admin>
* <http://localhost:8080/rest-without-root/rest/item/index>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/vaadin-on-grails/rest/without_using_root.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
