Simple demo of Getting started with Springboot

Source: Internet
Author: User
Tags apache tomcat

Project Building

We used MAVEN to build the Springboot project, first creating a MAVEN project with the corresponding POM file as follows:

<properties> <java.version>1.8</java.version> </properties> <!--The parent dependency of the integrated Springboot--<parent> <groupid>org. Springframework.boot</groupid> <artifactId>spring-boot-starter-parent</artifactId> <ver Sion>1.5.7. release</version> </parent> <dependencies> <dependency> <groupid>org .springframework.boot</groupid> <artifactId>spring-boot-starter-web</artifactId> </ Dependency> </dependencies> <!--play executable jar--<build> <plugins> &LT;PL Ugin> <groupId>org.springframework.boot</groupId> <artifactid>spring-bo ot-maven-plugin</artifactid> </plugin> </plugins> </build>

Create Application.java

Package Com.ysl;import Org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication Public Class  Application {    publicstaticvoid  main (string[] args) {        Springapplication.run (Application. class , args);}    }

Run the main method and the result is as follows:

  . ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ |'_ | '_| |'_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/: : Spring Boot:: (v1.5.7. RELEASE)2018-Geneva-Geneva Ten: -:24.028INFO6358---[main] com.ysl.Application:Starting application on master with PID6358(/home/workspace/springboottest/target/classes started by YSLinch/home/workspace/springboottest)2018-Geneva-Geneva Ten: -:24.038INFO6358---[main] com.ysl.Application:No active profileSet, falling back todefaultProfilesdefault2018-Geneva-Geneva Ten: -:24.218INFO6358---[main] ationconfigembeddedwebapplicationcontext:refreshing Org.springframework.boot[email protected]1b372 88:startup Date [Sat MarGeneva Ten: -: -Cst2018]; Root of context Hierarchy2018-Geneva-Geneva Ten: -:27.001INFO6358---[main] s.b.c.e.t.tomcatembeddedservletcontainer:tomcat initialized with Port (s):8080(HTTP)2018-Geneva-Geneva Ten: -:27.025INFO6358---[main] o.apache.catalina.core.standardservice:starting service [Tomcat]2018-Geneva-Geneva Ten: -:27.026INFO6358---[main] org.apache.catalina.core.StandardEngine:Starting Servlet engine:apache tomcat/8.5. -2018-Geneva-Geneva Ten: -:27.141INFO6358---[ost-startstop-1] O.a.c.c.c.[tomcat]. [localhost]. [/]: Initializing Spring embedded Webapplicationcontext2018-Geneva-Geneva Ten: -:27.142INFO6358---[ost-startstop-1] O.s.web.context.contextloader:root Webapplicationcontext:initialization completedinch 2936Ms2018-Geneva-Geneva Ten: -:27.294INFO6358---[ost-startstop-1] o.s.b.w.servlet.servletregistrationbean:mapping servlet:'Dispatcherservlet'to [/]2018-Geneva-Geneva Ten: -:27.299INFO6358---[ost-startstop-1] o.s.b.w.servlet.filterregistrationbean:mapping Filter:'Characterencodingfilter'To: [/*]2018-03-03 10:28:27.300 INFO 6358---[ost-startstop-1] o.s.b.w.servlet.filterregistrationbean:mapping filter: ' Hidd Enhttpmethodfilter ' to: [/*]2018-03-03 10:28:27.300 INFO 6358---[ost-startstop-1] O.s.b.w.servlet.filterregistrationbean:mapping filter: ' Httpputformcontentfilter ' to: [/*]2018-03-03 10:28:27.300 in FO 6358---[ost-startstop-1] o.s.b.w.servlet.filterregistrationbean:mapping filter: ' Requestcontextfilter ' to: [/*]20 18-03-03 10:28:27.669 INFO 6358---[main] s.w.s.m.m.a.requestmappinghandleradapter:looking for @ControllerA Dvice:org.springframework.boot[email protected]1b37288:startup Date [Sat Mar 10:28:24 CST 2018];  Root of context hierarchy2018-03-03 10:28:27.755 INFO 6358---[main] 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) 2018-03-03 10:28:27.756 INFO 6358---[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) 2018-03-03 10:28:27.795 INFO 6358---[main] o.s.w.s.handler.simpleurlhandlermapping:mapped URL path [/WEBJ ars/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]2018-03-03 10:28:27.796 INFO 6358---[main] o.s.w.s.handler.simpleurlhandlermapping:mapped URL path [/**] onto handle R of Type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]2018-03-03 10:28:27.835 INFO 6358---[main] o.s.w.s.handler.simpleurlhandlermapping:mapped URL path [/**/Favicon.ico] onto handler of type [classOrg.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-Geneva-Geneva Ten: -:28.143INFO6358---[main] o.s.j.e.a.annotationmbeanexporter:registering beans forJMX exposure on startup2018-Geneva-Geneva Ten: -:28.252INFO6358---[main] s.b.c.e.t.tomcatembeddedservletcontainer:tomcat started on port (s):8080(HTTP)2018-Geneva-Geneva Ten: -:28.261INFO6358---[main] com.ysl.Application:Started applicationinch 5.148Seconds (JVM running for 5.974)

Spring boot has been started, the embedded tomcat container, the listener as 8080 a port, a springboot program is so simple to be created.

@SpringBootApplication annotations

The source code for springbootapplication annotations is as follows:

@Target ({elementtype.type}) @Retention (retentionpolicy.runtime) @ Documented@inherited@springbootconfiguration@enableautoconfiguration@componentscan (ExcludeFilters={@Filter (type=Filtertype.custom, Classes= {Typeexcludefilter.class}), @Filter (type=Filtertype.custom, Classes= {Autoconfigurationexcludefilter.class})}) Public@interface springbootapplication {@AliasFor (annotation= Enableautoconfiguration.class, Attribute="Exclude") Class<?>[] Exclude ()default {}; @AliasFor (Annotation= Enableautoconfiguration.class, Attribute="Excludename") string[] Excludename ()default {}; @AliasFor (Annotation= Componentscan.class, Attribute="basepackages") string[] scanbasepackages ()default {}; @AliasFor (Annotation= Componentscan.class, Attribute="basepackageclasses") Class<?>[] Scanbasepackageclasses ()default {};}

@Configuration: Indicates Application existence as a sprig configuration file
@EnableAutoConfiguration: Start spring boot built-in automatic configuration
@ComponentScan: Scan the bean, where the path is the Application package sub-path of the class and below, where the package com.ysl spring boot Bean is placed under the path already sub-path.

Building a Rest Project

The above operation even a HelloWorld did not come out, far enough to fill our needs. Under COM.YSL, create a child package controller, under which the Testcontroller.java is created, as follows:

Package Com.ysl.controller;import Org.springframework.web.bind.annotation.mapping;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RestController; @RestControllerpublicclass  TestController {    @RequestMapping ("/")    public  String Home () {        return"hello,springboot";    }}

Restart the program, Access http:localhost:8080, get results: hello,springboot

Simple demo of Getting started with springboot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.