前提条件
系统需要具有以下创建Spring Boot应用程序的最低要求 -
1.Java 72.Maven 3.23.Gradle 2.5
Spring Boot CLI
Spring Boot CLI是一个命令行工具,它用于运行Groovy脚本。它是使用Spring Boot命令行界面创建Spring Boot应用程序的最简单方法。可以在命令提示符下创建,运行和测试应用程序。
本节介绍手动安装Spring Boot CLI所涉及的步骤。如需进一步的帮助,可以使用以下链接:
代码语言:javascript复制https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-installing-spring-boot
还可以从Spring Software存储库下载Spring CLI发行版:
代码语言:javascript复制https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-manual-cli-installation
对于手动安装,需要使用以下两个文件夹 -
•spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin.zip•spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin.tar.gz
下载后,解压缩归档文件并按照install.txt文件中给出的步骤进行操作。并不是说它不需要任何环境设置。
在Windows中,转到命令提示符下的Spring Boot CLI bin目录并运行命令spring --version以确保正确安装了spring CLI。执行命令后,可以看到spring CLI版本,如下所示 -
使用Groovy运行Hello World
创建一个包含Rest Endpoint脚本的简单groovy文件:hello.groovy,并使用spring boot CLI运行groovy文件。代码如下所示 -
代码语言:javascript复制@Controller
class Example {
@RequestMapping("/")
@ResponseBody
public String hello() {
"Hello Spring Boot"
}
}
请注意,在这个示例中,已经配置Spring Boot CLI bin目录到系统环境变量,并把文件:hello.groovy保存到D:/worksp/springboot/目录。使用命令spring run hello.groovy运行应用程序,如下面所示 -
代码语言:javascript复制D:workspspringboot>spring run hello.groovy
执行结果如下所示:
代码语言:javascript复制D:workspspringboot>spring run hello.groovy
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.5.RELEASE)
2018-09-26 09:34:13.782 INFO 13908 --- [ runner-0] o.s.boot.SpringApplication : Starting application on DESKTOP-CAN8JLM with PID 13908 (started by hema in D:workspspringboot)
2018-09-26 09:34:13.810 INFO 13908 --- [ runner-0] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2018-09-26 09:34:14.747 INFO 13908 --- [ runner-0] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2ec39a62: startup date [Wed Sep 26 09:34:14 CST 2018]; root of context hierarchy
2018-09-26 09:34:18.655 INFO 13908 --- [ runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-09-26 09:34:18.735 INFO 13908 --- [ runner-0] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-09-26 09:34:18.736 INFO 13908 --- [ runner-0] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-09-26 09:34:18.756 INFO 13908 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:Program Files (x86)Common FilesOracleJavajavapath;C:WINDOWSSunJavabin;C:WINDOWSsystem32;C:WINDOWS;C:Program Files (x86)Common FilesOracleJavajavapath;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program Files (x86)PuTTY;C:Program Filesdotnet;C:Program FilesMicrosoft SQL Server130ToolsBinn;C:WINDOWSSystem32OpenSSH;D:wampbinphpphp7.0.29;C:Program Files (x86)nodejs;D:softwarespring-2.0.5.RELEASEbin;D:Program FilesPython3Scripts;D:Program FilesPython3;C:UsershemaAppDataLocalMicrosoftWindowsApps;C:UsershemaAppDataRoamingnpm;.]
2018-09-26 09:34:19.030 INFO 13908 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader : Unknown loader org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@24ef272d class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader
2018-09-26 09:34:19.140 INFO 13908 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-09-26 09:34:19.140 INFO 13908 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4398 ms
2018-09-26 09:34:19.303 INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-09-26 09:34:19.311 INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-09-26 09:34:19.318 INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-09-26 09:34:19.320 INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-09-26 09:34:19.322 INFO 13908 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-26 09:34:19.556 INFO 13908 --- [ runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-26 09:34:19.831 INFO 13908 --- [ runner-0] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2ec39a62: startup date [Wed Sep 26 09:34:14 CST 2018]; root of context hierarchy
2018-09-26 09:34:19.998 INFO 13908 --- [ runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String Example.hello()
2018-09-26 09:34:20.004 INFO 13908 --- [ runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-09-26 09:34:20.006 INFO 13908 --- [ runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-09-26 09:34:20.050 INFO 13908 --- [ runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-26 09:34:20.053 INFO 13908 --- [ runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-26 09:34:20.651 INFO 13908 --- [ runner-0] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-09-26 09:34:20.821 INFO 13908 --- [ runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-09-26 09:34:20.830 INFO 13908 --- [ runner-0] o.s.boot.SpringApplication : Started application in 9.092 seconds (JVM running for 15.353)
运行groovy文件,所需的依赖项将自动下载,它将在Tomcat 8080端口启动应用程序,如下面给出的屏幕截图所示 -
Tomcat启动后,转到Web浏览器并点击URL => http://localhost:8080/,可以看到输出,如图所示: