Serwer Spring Boot w jednym pliku.

W pełni funkcjonalny serwer możemy utworzyć za pomocą tylko jednego pliku server.groovy.

Wymagania:

  • Java SDK 8
  • Groovy SDK

Przepis

Dodać plik server.groovy

@Grapes([
        @GrabConfig(systemClassLoader = true),
        @Grab(group = 'org.springframework.boot', module = 'spring-boot-starter-web', version = '2.2.6.RELEASE'),
        @GrabExclude(group = 'org.springframework.boot', module = 'spring-boot-starter-tomcat'),
        @Grab(group = 'org.springframework.boot', module = 'spring-boot-starter-undertow', version = '2.2.6.RELEASE')
])

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.web.bind.annotation.*
import org.springframework.boot.SpringApplication

/**
 * @author akrystian
 */
@SpringBootApplication
@RestController
class App {

    @RequestMapping("/")
    String simpleEndpoint() {
        return "Hello!!"
    }
}

static void main(String[] args) {
    SpringApplication.run(App.class, args)
}

Uruchomienie

groovy server.groovy

Logi z uruchomienia

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-04-04 02:28:52.463  INFO 32601 --- [           main] groovy.ui.GroovyMain                     : Starting GroovyMain v2.5.5 on Legion with PID 32601 (/home/krystian/.sdkman/candidates/groovy/current/lib/groovy-2.5.5.jar started by krystian in /home/krystian/IdeaProjects/code.conference.site)
2020-04-04 02:28:52.469  INFO 32601 --- [           main] groovy.ui.GroovyMain                     : No active profile set, falling back to default profiles: default
2020-04-04 02:28:53.391  WARN 32601 --- [           main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2020-04-04 02:28:53.411  INFO 32601 --- [           main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext
2020-04-04 02:28:53.411  INFO 32601 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 871 ms
2020-04-04 02:28:53.586  INFO 32601 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-04 02:28:53.716  WARN 32601 --- [           main] .b.a.g.t.GroovyTemplateAutoConfiguration : Cannot find template location: classpath:/templates/ (please add some templates, check your Groovy configuration, or set spring.groovy.template.check-template-location=false)
2020-04-04 02:28:53.859  INFO 32601 --- [           main] io.undertow                              : starting server: Undertow - 2.0.30.Final
2020-04-04 02:28:53.866  INFO 32601 --- [           main] org.xnio                                 : XNIO version 3.3.8.Final
2020-04-04 02:28:53.873  INFO 32601 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.3.8.Final
2020-04-04 02:28:53.994  INFO 32601 --- [           main] o.s.b.w.e.u.UndertowServletWebServer     : Undertow started on port(s) 8080 (http) with context path ''
2020-04-04 02:28:53.998  INFO 32601 --- [           main] groovy.ui.GroovyMain                     : Started GroovyMain in 1.996 seconds (JVM running for 4.051)
2020-04-04 02:29:32.110  INFO 32601 --- [  XNIO-1 task-1] io.undertow.servlet                      : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-04-04 02:29:32.110  INFO 32601 --- [  XNIO-1 task-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-04-04 02:29:32.118  INFO 32601 --- [  XNIO-1 task-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms

Test działania

> curl -i http://localhost:8080/
  HTTP/1.1 200 OK
  Connection: keep-alive
  Content-Type: text/plain;charset=UTF-8
  Content-Length: 7
  Date: Sat, 04 Apr 2020 00:42:57 GMT

  Hello!!%