1. 程式人生 > >SpringBoot入門-1(Hello Word Boot)

SpringBoot入門-1(Hello Word Boot)

eas hand running 頁面 .cn tpm 2.3 src size

  1、創建一個Maven項目,添加一個parent,代碼如下

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.4.RELEASE</version>
    </parent>

  2、添加一個依賴

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId> 
        </dependency>
        
 </dependencies>    

  3、配置java版本,若不填,默認1.6

 <build>
<!-- 配置java版本 不配置的話默認父類配置的是1.6-->
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  4、新建一個java文件

  

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @RestController [email protected]
/* */@RequestBody * */ @RestController @EnableAutoConfiguration public class HelloController { @RequestMapping("/hello") public String hello(){ return "Hello Word SpringBoot"; } public static void main(String[] args) { SpringApplication.run(HelloController.class, args); } }

  最後run運行

  

  .   ____          _            __ _ _
 /\\ / ___‘_ __ _ _(_)_ __  __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  ‘  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.4.RELEASE)

2017-08-12 13:51:00.098  INFO 2124 --- [           main] com.test.HelloController                 : Starting HelloController on DESKTOP-M8DR5S3 with PID 2124 (F:\MyEclipse\mywork\spinrBoot-hello\target\classes started by 592302116 in F:\MyEclipse\mywork\spinrBoot-hello)
2017-08-12 13:51:00.113  INFO 2124 --- [           main] com.test.HelloController                 : No active profile set, falling back to default profiles: default
2017-08-12 13:51:00.160  INFO 2124 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbedde[email protected]: startup date [Sat Aug 12 13:51:00 CST 2017]; root of context hierarchy
2017-08-12 13:51:01.770  INFO 2124 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-12 13:51:01.815  INFO 2124 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-08-12 13:51:01.815  INFO 2124 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.33
2017-08-12 13:51:01.955  INFO 2124 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-08-12 13:51:01.955  INFO 2124 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1795 ms
2017-08-12 13:51:02.299  INFO 2124 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: ‘dispatcherServlet‘ to [/]
2017-08-12 13:51:02.315  INFO 2124 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: ‘characterEncodingFilter‘ to: [/*]
2017-08-12 13:51:02.315  INFO 2124 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*]
2017-08-12 13:51:02.315  INFO 2124 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: ‘httpPutFormContentFilter‘ to: [/*]
2017-08-12 13:51:02.315  INFO 2124 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: ‘requestContextFilter‘ to: [/*]
2017-08-12 13:51:02.565  INFO 2124 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbedde[email protected]: startup date [Sat Aug 12 13:51:00 CST 2017]; root of context hierarchy
2017-08-12 13:51:02.630  INFO 2124 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.test.HelloController.hello()
2017-08-12 13:51:02.630  INFO 2124 --- [           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)
2017-08-12 13:51:02.630  INFO 2124 --- [           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)
2017-08-12 13:51:02.708  INFO 2124 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-12 13:51:02.708  INFO 2124 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-12 13:51:02.802  INFO 2124 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-12 13:51:02.958  INFO 2124 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-08-12 13:51:03.083  INFO 2124 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-12 13:51:03.083  INFO 2124 --- [           main] com.test.HelloController                 : Started HelloController in 3.329 seconds (JVM running for 3.61)
2017-08-12 13:51:32.885  INFO 2124 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet ‘dispatcherServlet‘
2017-08-12 13:51:32.885  INFO 2124 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet ‘dispatcherServlet‘: initialization started
2017-08-12 13:51:32.900  INFO 2124 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet ‘dispatcherServlet‘: initialization completed in 15 ms

  在url輸入http://127.0.0.1:8080/hello

  頁面將展示出來

技術分享

版權聲明:本文為不會代碼的小白原創文章,未經允許不得轉載。

SpringBoot入門-1(Hello Word Boot)