Simply Groovy
- Mark Kendall
- 8 hours ago
- 3 min read
Okay, let's create a REST API in your Groovy project to serve a JSON file of customers. Here's how we can do it:
1. Create the Customer JSON File:
Let's name the JSON file `customers.json` and place it in the root of your project (or a logical `resources` directory if you have one).
```json
[
{
"id": 1,
"firstName": "Alice",
"lastName": "Smith",
"email": "alice.smith@example.com"
},
{
"id": 2,
"firstName": "Bob",
"lastName": "Johnson",
"email": "bob.johnson@example.com"
},
{
"id": 3,
"firstName": "Charlie",
"lastName": "Brown",
"email": "charlie.brown@example.com"
}
]
```
File Name: `customers.json`
2. Create the REST Controller:
We'll create a Groovy class that acts as the REST controller. Let's call it `CustomerController.groovy`.
```groovy
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.beans.factory.annotation.Value
import groovy.json.JsonSlurper
@RestController
class CustomerController {
@Value('classpath:customers.json')
Resource customersResource
@GetMapping('/api/customers')
List<Map> getAllCustomers() {
def slurper = new JsonSlurper()
return slurper.parse(customersResource.inputStream) as List<Map>
}
}
```
File Name: `CustomerController.groovy`
Explanation:
* `@RestController`: This annotation marks the class as a REST controller, making it capable of handling incoming web requests.
* `@Value('classpath:customers.json') Resource customersResource`: This injects a Spring `Resource` representing your `customers.json` file from the classpath.
* `@GetMapping('/api/customers')`: This maps HTTP GET requests to the `/api/customers` endpoint to the `getAllCustomers()` method.
* `JsonSlurper`: This Groovy class is used to parse the JSON content from the file.
* The `getAllCustomers()` method reads the content of `customers.json`, parses it into a list of maps (where each map represents a customer), and returns this list. Spring will automatically convert this list into a JSON response.
3. Update Your `build.gradle`:
To make this work, you'll need to include the Spring Boot Web dependency in your `build.gradle` file.
```gradle
plugins {
id 'groovy'
id 'org.springframework.boot' version '3.2.7' // Or your Spring Boot version
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'org.example'
version = '0.0.1-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.codehaus.groovy:groovy'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
application {
mainClass = 'your.package.YourMainClass' // Replace with the actual path to your main class
}
```
Important:
* Replace `'3.2.7'` with the Spring Boot version you want to use.
* Replace `'your.package.YourMainClass'` with the actual fully qualified name of your main Groovy class that currently runs the dice simulation.
4. Create a Spring Boot Application Main Class (if you don't have one):
If your existing main class isn't already a Spring Boot application entry point, you'll need to create one. Let's assume your existing main class is something like `DiceRoller.groovy`. You can create a separate Spring Boot application class:
```groovy
// src/main/groovy/com/example/DemoApplication.groovy (adjust package as needed)
package com.example
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class DemoApplication {
static void main(String[] args) {
SpringApplication.run(DemoApplication, args)
}
}
```
In your `build.gradle`, update the `mainClass` in the `application` block to point to this new application class (e.g., `'com.example.DemoApplication'`).
How to Run:
1. Make sure you have the `customers.json` file in the root of your project (or in `src/main/resources`).
2. Build your project using Gradle: `./gradlew build`
3. Run your Spring Boot application: `./gradlew bootRun`
Once the application is running, you can access the customer data by making a GET request to `http://localhost:8080/api/customers` in your web browser or using a tool like `curl`. You should receive a JSON response containing the customer data.
Regarding an existing `main` class:
You can keep your existing `main` class (`any.groovy`). The Spring Boot application will run independently, and the REST API will be accessible on the specified endpoint. You don't necessarily need to make your existing `main` class call the REST API (unless that's a specific requirement you have).
Let me know if you have any other questions or would like to modify this further!
コメント