The HelloWorld of Springboot

Source: Internet
Author: User


This example will be managed with MAVEN, code hosted on GitHub, address: https://github.com/wolf909867753/springboot.

1. Create Maven-module and add springboot dependencies in Pom.xml

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:s chemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <artifactId>helloWorld</artifactId> <packaging>war</

    packaging> <name>springboot-helloworld demo</name> <url>http://maven.apache.org</url>
        <!--Spring Boot start parent dependency--> <parent> <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version>
            </parent> <dependencies> <!--Spring Boot Web dependencies--> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-web</art

Ifactid> </dependency>        <!--Junit--> <dependency> <groupId>junit</groupId> <a Rtifactid>junit</artifactid> <version>4.12</version> &LT;SCOPE&GT;TEST&LT;/SC ope> </dependency> </dependencies> <build> <finalname>helloworld</fi


 Nalname> </build> </project>
2. Create Helloworldcontroller class
@RestController public
class HelloWorld {

    @RequestMapping ("/") Public
    String HelloWorld () {return
        " Hello world! ";
    }

}

3. Test class
@SpringBootApplication public
class HelloWorldTest {public
        static void Main (string[] args) {
            Springapplication.run (Helloworldtest.class,args);
        }
Run the main method to access the http://127.0.0.1:8080 page output Hello world!

Summary:
/** * One of the simplest Web application test Classes reference the Helloworldtest.java * using the Spring boot framework can greatly speed up the development of Web applications. First, introduce Spring-boot-starter-web * Run applications in MAVEN project dependencies: MVN Spring-boot:run or run the main () method in the IDE, * Access http://localhost in the browser
 : 8080,hello world! appears on the page.
 * Only a few more than 10 lines of Java code, a Hello World application can run correctly, then what this code does. * From the entrance of the program Springapplication.run (Helloworld.class, args); Start Analysis: * Springapplication is the class that describes the spring application in the Spring boot framework, * its run ( Method creates a spring application context (application).  On the other hand, it scans for dependencies on the current application class path, * For example, in this case, SPRING-WEBMVC (introduced by Spring-boot-starter-web) is found in the classpath, so spring boot will determine that this is a Web application, *
 and starts an inline servlet container (the default is Tomcat) for processing HTTP requests. * The Spring WEBMVC Framework distributes HTTP requests received in the Servlet container to the corresponding @controller classes, * @RestController is a special kind of @controller, and its return value is directly as an HTTP
 The body part of the response is returned to the browser.
 * @RequestMapping annotations indicate that the method handles HTTP requests corresponding to those URLs, which is what we often call URL routing (routing), where the distribution of the request is done with spring.
 * For example, the http://localhost:8080/root path in the above code is routed to the HelloWorld () method for processing.
 * If you access Http://localhost:8080/hello, a 404 Not Found error occurs because we have not written any method to handle the/hello request. * * @SpringBootApplicatIon-----Spring Boot Application Startup class * In this case you can also remove @springbootapplication use the following code directly or run * Created by Bysocket on 16/4/26. @SpringBootApplication public class HelloWorldTest {public static void main (string[] args) {Spri
        Ngapplication.run (Helloworldtest.class,args); If the @restcontroller----> @Controller, running Main method will report the following error * Whitelabel error Page this application h
    As no explicit mapping for/error, so your are seeing this as a fallback.
    Sat June 14:30:28 CST 2017 There was a unexpected error (Type=not Found, status=404). No Message Available * * * *



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.