When we used the spring framework before, we needed to add dependency dependencies to the Pom file, and then create a new spring-related XML file, often with fewer XML files. Then continue to use Tomcat or jetty as a container to run the project. Basically creating a new project every time is such a process, and we sometimes just want to quickly create a spring Web project to test something, or to save time.
Now we can do this quickly with spring boot.
1. We create a MAVEN project, the project name is Spring-boot-helloworld, and then modify the Pom.xml file:
<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupId>Net.mingyang</groupId> <Artifactid>Spring-boot-helloworld</Artifactid> <version>0.0.1-snapshot</version> <Packaging>Jar</Packaging> <name>Spring-boot-helloworld</name> <Description>Demo Project for Spring Boot</Description> <Parent> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-parent</Artifactid> <version>1.4.1.RELEASE</version> <RelativePath/> <!--Lookup parent from repository - </Parent> <Properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </Properties> <Dependencies> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-web</Artifactid> </Dependency> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-test</Artifactid> <Scope>Test</Scope> </Dependency> </Dependencies> <Build> <Plugins> <plugin> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-maven-plugin</Artifactid> </plugin> </Plugins> </Build></Project>
2. Create a new controller to accept the processing of our request:
PackageNet.mingyang.spring_boot_helloworld;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;Importorg.springframework.web.bind.annotation.ResponseBody, @SpringBootApplication @controller Public classSimplecontroller {@RequestMapping (value= "/hello", method =requestmethod.get) @ResponseBody PublicString Hello () {return"Hello World"; } Public Static voidMain (string[] args) {Springapplication.run (simplecontroller.class, args); }}
3. Execute the main () method in Simlecontroller:
. ____ _ __ _ _ /\\/___ ' _ __ _ _ (_) _ __ _ \ \ \ (() \___ | ' _ | ' _| | ' _ \ _ ' | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____|. __|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/: : Spring Boot:: (v1.4.1. RELEASE)2016-10-18 08:43:46.946 INFO 4640---[main] n.m.s.simplecontroller:starting SimpleControl Ler on D60601653 with PID 4640 (X:\dev\spring-test-suite\spring-boot-helloworld\target\classes started by Lbin in X:\dev\ spring-test-suite\spring-boot-HelloWorld)2016-10-18 08:43:46.950 INFO 4640---[main] n.m.s.simplecontroller:no active profile set, Falling Back todefaultProfilesdefault2016-10-18 08:43:47.238 INFO 4640---[main] ationconfigembeddedwebapplicationcontext:refreshing ORG.SPRINGF Ramework.boot[email protected]69cc1627:startup Date [Tue Oct 08:43:47 CST 2016]; Root of context Hierarchy2016-10-18 08:43:49.969 INFO 4640---[main] s.b.c.e.t.tomcatembeddedservletcontainer:tomcat initialized wit H Port (s): 8080(HTTP)2016-10-18 08:43:49.983 INFO 4640---[main] o.apache.catalina.core.standardservice:starting service Tomcat2016-10-18 08:43:49.984 INFO 4640---[main] org.apache.catalina.core.StandardEngine:Starting Servlet Engin E:apache tomcat/8.5.52016-10-18 08:43:50.108 INFO 4640---[ost-startstop-1] o.a.c.c.c.[tomcat]. [localhost]. [/]: Initializing Spring embedded Webapplicationcontext2016-10-18 08:43:50.109 INFO 4640---[ost-startstop-1] o.s.web.context.contextloader:root WebApplicationCon Text:initialization completed in 2874Ms2016-10-18 08:43:50.334 INFO 4640---[ost-startstop-1] o.s.b.w.servlet.servletregistrationbean:mapping servlet: ' disp Atcherservlet ' to [/]2016-10-18 08:43:50.340 INFO 4640---[ost-startstop-1] o.s.b.w.servlet.filterregistrationbean:mapping filter: ' Chara Cterencodingfilter ' to: [/*]2016-10-18 08:43:50.340 INFO 4640---[ost-startstop-1] o.s.b.w.servlet.filterregistrationbean:mapping filter: ' Hidd Enhttpmethodfilter ' to: [/*]2016-10-18 08:43:50.340 INFO 4640---[ost-startstop-1] O.s.b.w.servlet.filterregistrationbean:mapping filter: ' Httpputformcontentfilter ' to: [/*]2016-10-18 08:43:50.340 in FO 4640---[ost-startstop-1] o.s.b.w.servlet.filterregistrationbean:mapping filter: ' Requestcontextfilter ' to: [/*]20 16-10-18 08:43:50.766 INFO 4640---[main] s.w.s.m.m.a.requestmappinghandleradapter:looking for @ControllerA Dvice:org.springframework.boot[email protected]69cc1627:startup Date [Tue Oct 08:43:47 CST 2016]; Root of context hierarchy2016-10-18 08:43:50.863 INFO 4640---[main] s.w.s.m.m.a.requestmappinghandlermapping : Mapped "{[/hello],methods=[get]}" onto public java.lang.String Net.mingyang.spring_boot_helloworld. Simplecontroller.hello () 2016-10-18 08:43:50.868 INFO 4640---[main] 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.BasicErrorController.error (Javax.servlet.http.HttpServletRequest) 2016-10-18 08:43:50.868 INFO 4640---[main] s.w.s.m.m.a.requestmappinghandlermapping:mapped "{[/error],prod Uces=[text/html]} "onto public org.springframework.web.servlet.ModelAndView Org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml ( Javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse) 2016-10-18 08:43:50.912 INFO 4640--- [main] o.s.w.s.handler.simpleurlhandlermapping:mapped URL path [/webjars/**] onto handler of type [class org . springframework.web.servlet.resource.resourcehttprequesthandler]2016-10-18 08:43:50.912 INFO 4640---[main] o.s.w.s.handler.simpleurlhandlermapping:mapped URL Path [/**] onto handler of type [Class Org.springframework.web.servlet.resource.resourcehttprequesthandler]2016-10-18 08:43:51.029 INFO 4640---[main] o . s.w.s.handler.simpleurlhandlermapping:mapped URL Path [/**/Favicon.ico] onto handler of type [classOrg.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-10-18 08:43:51.767 INFO 4640---[main] o.s.j.e.a.annotationmbeanexporter:registering beans forJMX exposure on startup2016-10-18 08:43:51.878 INFO 4640---[main] s.b.c.e.t.tomcatembeddedservletcontainer:tomcat started on port (s): 8080(HTTP)2016-10-18 08:43:51.884 INFO 4640---[main] n.m.s.simplecontroller:started Simplecontroll Er in 5.658 seconds (JVM running for5.99)
View logs can be found by default Tomcat, port binding at 8080, Access: Http://localhost:8080/hello, you can see the output of our code words.
Isn't it simple?
Spring Boot rapid Development Web project