Spring Boot CLI - Merhaba Dünya Örneği
Bu örnekte Spring Boot + MVC + Rest tabanlı bir Web uygulaması oluşturacağız.
1. Adım: Kaynak Klasör Oluşturun
FirstApplication klasörü oluşturun E:\Test folder.
2. Adım: Kaynak Dosya Oluşturun
FirstApplication.groovy dosyasını içinde oluşturun E:\Test folder aşağıdaki kaynak kodu ile -
@RestController
class FirstApplication {
@RequestMapping("/")
String welcome() {
"Welcome to TutorialsPoint.Com"
}
}
3. Adım: Uygulamayı çalıştırın
Aşağıdaki komutu yazın -
E:/Test/> spring run FirstApplication.groovy
Şimdi Spring Boot CLI devreye girecek, gerekli bağımlılıkları indirecek, gömülü tomcat'i çalıştıracak, uygulamayı dağıtacak ve başlatacak. Konsolda aşağıdaki çıktıyı görebilirsiniz -
Resolving dependencies..........................................................
................................................................................
........................
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _> | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.8.RELEASE)
2017-11-07 17:36:55.703 INFO 5528 --- [ runner-0] o.s.boot.SpringApplication:
Starting application on ...
2017-11-07 17:36:55.707 INFO 5528 --- [ runner-0] o.s.boot.SpringApplication:
No active profile set, falling back to default profiles: default
2017-11-07 17:36:56.067 INFO 5528 --- [ runner-0] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4c108392: startup date [Tue Nov 07 17:36:
56 IST 2017]; root of context hierarchy
2017-11-07 17:36:57.327 INFO 5528 --- [ runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-11-07 17:36:57.346 INFO 5528 --- [ runner-0] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-11-07 17:36:57.354 INFO 5528 --- [ runner-0] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23
2017-11-07 17:36:57.537 INFO 5528 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader : Unknown loader org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@41bfad4f class org.springframew
ork.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader
2017-11-07 17:36:57.567 INFO 5528 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-11-07 17:36:57.567 INFO 5528 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1500 ms
2017-11-07 17:36:57.725 INFO 5528 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-11-07 17:36:57.730 INFO 5528 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-11-07 17:36:57.730 INFO 5528 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-11-07 17:36:57.730 INFO 5528 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-11-07 17:36:57.730 INFO 5528 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-11-07 17:36:58.012 INFO 5528 --- [ runner-0] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4c108392: startup date [Tue Nov 07 17:36:56 IST 2017]; root of context hierarchy
2017-11-07 17:36:58.066 INFO 5528 --- [ runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String FirstApplication.home()
2017-11-07 17:36:58.070 INFO 5528 --- [ runner-0] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity
> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2017-11-07 17:36:58.071 INFO 5528 --- [ 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 .BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2017-11-07 17:36:58.096 INFO 5528 --- [ runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-11-07 17:36:58.096 INFO 5528 --- [ runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-11-07 17:36:58.129 INFO 5528 --- [ runner-0] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-11-07 17:36:58.626 INFO 5528 --- [ runner-0] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017-11-07 17:36:58.696 INFO 5528 --- [ runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2017-11-07 17:36:58.699 INFO 5528 --- [ runner-0] o.s.boot.SpringApplication : Started application in 3.529 seconds (JVM running for 190.196) 2017-11-07 17:37:20.217 INFO 5528 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2017-11-07 17:37:20.218 INFO 5528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2017-11-07 17:37:20.238 INFO 5528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 20 ms
4. Adım: Tarayıcıda uygulamaya göz atın
Yay bazlı dinlenme uygulamamız artık hazır. URL'yi "olarak aç"http://localhost:8080/"ve aşağıdaki çıktıyı göreceksiniz -
Hello World
Önemli noktalar
Spring CLI'nin nasıl çalıştığını anlamak için aşağıdaki noktaları göz önünde bulundurun.
Tüm bağımlılık JAR'ları yalnızca ilk kez indirilir.
Spring CLI, kodda kullanılan sınıflara ve ek açıklamalara göre hangi bağımlılık JAR'larının indirileceğini otomatik olarak algılar.
Son olarak, kodun derlenmesinden sonra, savaş dosyasını gömülü bir tomcat üzerine konuşlandırın ve varsayılan 8080 bağlantı noktasında gömülü tomcat sunucusunu başlatın.