spring boot之Hello Word

In the last blog post, I introduced how to create a spring boot web project. In this blog post, I will introduce the hello word, the most important tool of spring boot. First, let's introduce a few important annotations.

1. @SpringBootApplication This annotation is used to quickly configure the startup class. This annotation is also quite similar to @Configuration+@EnableAutoConfiguration+@ComponentScan

2. The role of @EnableAutoConfiguration is to allow spring boot to automatically configure the spring framework according to the dependencies declared by the application.

3. The role of @ComponentScan is that the controller scans the scope of the package.

4. The role of @Configuration is equivalent to the beans configured in spring's xml

5. The role of @RestController is to mark a class as controller

Next, let's take a look at the code example,

First let's take a look at the code in the controller class, I created a TestController class. code show as below:

@RestController
public class TestController  {
    
    @GetMapping("/hello")
    public String hello(){
        return "Hello Word!";
    }
}

The code in the spring boot startup class is as follows.

@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages = "com")
public class DemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

Next start spring boot

Connected to the target VM, address: '127.0.0.1:51912', transport: 'socket'

                      _                       _
                     | |                     (_)            
  _ __ ___  _   _ ___| |__   __ _  ___   __ _ _  __ _ _ __  
 | '_ ` _ \| | | / __| '_ \ / _` |/ _ \ / _` | |/ _` | '_ \ 
 | | | | | | |_| \__ \ | | | (_| | (_) | (_| | | (_| | | | |
 |_| |_| |_|\__,_|___/_| |_|\__,_|\___/ \__, |_|\__,_|_| |_|
                                           | |              
                                           |_|
2.0.1.RELEASE
 (v2.0.1.RELEASE)

2018-04-10 15:13:21.800  INFO 12272 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on DESKTOP-EUKLDH3 with PID 12272 (E:\Project\springboot-plus\demo\target\classes started by msqxh in E:\Project\springboot-plus\demo)
2018-04-10 15:13:21.808  INFO 12272 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2018-04-10 15:13:21.962  INFO 12272 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@74e28667: startup date [Tue Apr 10 15:13:21 CST 2018]; root of context hierarchy
2018-04-10 15:13:25.656  INFO 12272 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-04-10 15:13:25.704  INFO 12272 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-10 15:13:25.704  INFO 12272 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-10 15:13:25.722  INFO 12272 --- [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: [F:\Program Files\java8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\TortoiseSVN\bin;F:\Program Files\Java7\jdk1.7.0_80\bin;F:\Program Files\Java7\jdk1.7.0_80\jre\bin;F:\Program Files\maven\apache-maven-3.5.0\bin;F:\Program Files\maven\apache-maven-3.5.0\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin;C:\Users\msqxh\AppData\Local\Microsoft\WindowsApps;;.]
2018-04-10 15:13:25.963  INFO 12272 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-10 15:13:25.964  INFO 12272 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4009 ms
2018-04-10 15:13:26.310  INFO 12272 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-04-10 15:13:26.322  INFO 12272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-10 15:13:26.323  INFO 12272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-04-10 15:13:26.324  INFO 12272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-04-10 15:13:26.324  INFO 12272 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-04-10 15:13:26.752  INFO 12272 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-10 15:13:27.563  INFO 12272 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@74e28667: startup date [Tue Apr 10 15:13:21 CST 2018]; root of context hierarchy
2018-04-10 15:13:27.749  INFO 12272 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.example.controller.TestController.hello()
2018-04-10 15:13:27.759  INFO 12272 --- [           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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-10 15:13:27.760  INFO 12272 --- [           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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-10 15:13:27.847  INFO 12272 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-10 15:13:27.847  INFO 12272 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-10 15:13:28.242  INFO 12272 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-10 15:13:28.366  INFO 12272 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-04-10 15:13:28.379  INFO 12272 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 7.553 seconds (JVM running for 9.182)
2018-04-10 15:13:47.281  INFO 12272 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-04-10 15:13:47.282  INFO 12272 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-04-10 15:13:47.353  INFO 12272 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 71 ms

According to the above log, it can be seen that the springboot started successfully, and the port is the default 8080.

Let's access our controller through this port, enter in the browser's address bar: http://localhost:8080/hello

The result we see is:

The hello word we returned has been output to the browser. Our springboot hello word journey has ended happily here.

This article is original, author: Mu Shaoqian

Reader corrections are welcome. Thanks!

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324481631&siteId=291194637