Quick Microservice Prototyping With Spring Boot CLI

Spring boot command line interface (CLI) can be used to quickly create microservice backend prototypes. If you are not familiar with spring boot cli, see our earlier articles - introduction to spring boot cli and introduction to spring boot framework. In this article, I will show you how quickly you can create spring based microservice prototypes with spring boot cli. Spring boot cli is an essential tool for Java web application developer.

Http Microservice Endpoints With JSON Output

The following groovy controller shows how a JSON HTTP REST endpoint for customer data can be quickly prototyped using spring boot cli,

@RestController
class Customers {

    class Customer {
        String email;
        String name;
    }

    @RequestMapping("/customers")
    Map home() {
        Map customers = new HashMap();
        customers.put("customers",getCustomers());
        return customers;
    }

    List<Customer> getCustomers() {
        List<Customer> list = new ArrayList();
        def c = new Customer();
        c.name = "QPT";
        c.email = "qpt@quickprogrammingtips.com";
        list.add(c);

        c = new Customer();
        c.name = "QPT2";
        c.email = "qpt2@quickprogrammingtips.com";
        list.add(c);

        return list;
    }
}

Run the above controller using the following command,

spring run *.groovy

Use the URL http://localhost:8080/customers to access the microservice endpoint.

Sending And Receiving Messages Through JMS Queue

It is easy to prototype message queues using spring boot cli. The following groovy file creates an Artemis embedded message queue and sends a message.

@Grab("spring-boot-starter-artemis")
@Grab("artemis-jms-server")

@RestController
@EnableJms
class JMSSender {

    @Autowired
    JmsTemplate jmsTemplate

    @RequestMapping("/send")
    String home() {
        def messageCreator = {
            session ->
            session.createObjectMessage("Hello: Via Artemis")
        } as MessageCreator
        jmsTemplate.send("spring-boot", messageCreator)

        return "Message Sent";
    }
}

The following groovy file listens on the Artemis embedded message queue and whenever a message is received it will output the message on the spring boot cli console.

@Grab("spring-boot-starter-artemis")
@Grab("artemis-jms-server")

@RestController
@EnableJms
@Log
class JMSReceiver {

    @Autowired
    JmsTemplate jmsTemplate

    @JmsListener(destination = 'spring-boot')
    def receive(String message) {
        log.info "Received ${message}"
    }
}

Run both groovy controllers using the following command.

spring run jmssender.groovy jmsreceiver.groovy

Access the URL http://localhost:8080/send with the spring boot console visible. You should see the message being processed by the queue listener in the console.

Using MongoDB Database Backend with Microservice Endpoints

You can easily integrate the microservice prototype with an existing database. The following example groovy file (mongodemo.groovy) shows how to connect to a local MongoDB instance. This example assumes that a MongoDB instance is up and running on the local machine/default port with the "test" database and a table named "user" with attributes "name" and "email". Add a number of table entries before running the program below.

package com.quickprogrammingtips.demo

@Grab("spring-boot-starter-data-mongodb")
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.data.annotation.Id;

@RestController
class MongoDemo {

    @Autowired
    UserRepository repo;

    @RequestMapping("/users")
    List home() {
        return repo.findAll();
    }
}

// By default maps to user table in Mongo
class User {
    @Id
    String id;
    String name;
    String email;
}
interface UserRepository extends MongoRepository<User, String> {
}

Note that the above groovy file uses a package (com.quickprogrammingtips.demo). This is required for auto scanning/auto creation of Mongo repository class (default package won't work). Also we cannot run the above application using spring run since scanning of repository classes cannot work with groovy scripts. The generated classes are required for auto scanning. Hence to run the above example, we need to create a jar and then execute the jar.

spring jar app.jar mongodemo.groovy

java -jar app.jar

Access the URL http://localhost:8080/users to get a list of users from the database table. Voila! Now you have a JSON based microservices endpoint with MongoDB connectivity!

References