Spring Boot Making CLI Application using Groovy

Points To Remember

You can make the cli application without writing any configurations or xml's that you needed to do in traditional spring framework.
Spring boot automatically adds the boiler plate code to the application that may look like this

// import org.springframework.web.bind.annotation.Controller
// other imports ...
// @Grab("org.springframework.boot:spring-boot-web-starter:0.5.0")
// @EnableAutoConfiguration

@RestController
@RequestMapping("/")
class Hello {

 @RequestMapping(value="/hello")
  public String index(){
    return "<h1>Hello World !! Today's date is " + new Date() + "</h1>";
  }

// public static void main(String[] args) {
// SpringApplication.run(Example.class, args);
// }

}

The following code is a self sufficient code to run in a servlet container.

Hello.groovy

import java.util.Date;

@RestController
@RequestMapping(value="/")
class Hello{
 
  @RequestMapping(value="/hello")
  public String index(){
    return "<h1>Hello World !! Today's date is " + new Date() + "</h1>";
  }

}
Run the above code using the command

spring run Hello.groovy

You will be able to see the result in the browser as shown in the image below



No comments :

Post a Comment