Spring boot Hello word 教程

对于开发界 万事都是开头 Hellword, 我们也来一个spring boot Helloword 教程!

  • 第一步,当然是在pom.xml 引入 spring boot jar, 我使用的是 undertow

    org.springframework.boot spring-boot-starter-parent 1.5.22.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-undertow

完整pom.xml 配置

<project xmlns="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>com.pangugle</groupId>
  <artifactId>testspringboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testspringboot</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

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

  <dependencies>

    <!-- spring -->
		<dependency>
	      <groupId>org.springframework.boot</groupId>
	      <artifactId>spring-boot-starter-web</artifactId>
	      <exclusions>
	        <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-tomcat</artifactId>
	        </exclusion>
		  </exclusions>
	  	</dependency>

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

  </dependencies>
</project>

编写spring boot main 方法启动器

package com.pangugle.testspringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

上面我们使用Annotaion 注解

@SpringBootApplication

来配置spring boot 启动器; 然后就是

SpringApplication.run(App.class, args);

这句就是启动 spring boot 框架,并运行服务,服务端口为8080,这是默认配置的!

编写一个 Helloword mvc, 并在浏览器上访问

package com.pangugle.testspringboot;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@RequestMapping("/sayHello")
	public String sayHello()
	{
		return "hello spring boot!";
	}
}
  • @RestController 注解表示返回一个字符串,不返回页面
  • @RequestMapping 注解表示 访问路径,
    • sayHello 访问url : 127.0.0.1:8080/sayHello

注意: spring boot 默认端口是 8080

运行 App,在控制台输出如下:


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

2020-09-12 13:37:12.398  INFO 14068 --- [           main] com.pangugle.testspringboot.App          : Starting App on SD-20191007VAAX with PID 14068 (D:\dev\workspace\testspringboot\target\classes started by Administrator in D:\dev\workspace\testspringboot)
2020-09-12 13:37:12.400  INFO 14068 --- [           main] com.pangugle.testspringboot.App          : No active profile set, falling back to default profiles: default
2020-09-12 13:37:12.429  INFO 14068 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3d8314f0: startup date [Sat Sep 12 13:37:12 CST 2020]; root of context hierarchy
2020-09-12 13:37:13.285  INFO 14068 --- [           main] org.xnio                                 : XNIO version 3.3.8.Final
2020-09-12 13:37:13.297  INFO 14068 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.3.8.Final
2020-09-12 13:37:13.395  WARN 14068 --- [           main] io.undertow.websockets.jsr               : UT026009: XNIO worker was not set on WebSocketDeploymentInfo, the default worker will be used
2020-09-12 13:37:13.395  WARN 14068 --- [           main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2020-09-12 13:37:13.411  INFO 14068 --- [           main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext
2020-09-12 13:37:13.412  INFO 14068 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 983 ms
2020-09-12 13:37:13.465  INFO 14068 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2020-09-12 13:37:13.468  INFO 14068 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2020-09-12 13:37:13.469  INFO 14068 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-09-12 13:37:13.469  INFO 14068 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2020-09-12 13:37:13.469  INFO 14068 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2020-09-12 13:37:13.636  INFO 14068 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3d8314f0: startup date [Sat Sep 12 13:37:12 CST 2020]; root of context hierarchy
2020-09-12 13:37:13.677  INFO 14068 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/sayHello]}" onto public java.lang.String com.pangugle.testspringboot.HelloController.sayHello()
2020-09-12 13:37:13.679  INFO 14068 --- [           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)
2020-09-12 13:37:13.679  INFO 14068 --- [           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)
2020-09-12 13:37:13.695  INFO 14068 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-09-12 13:37:13.695  INFO 14068 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-09-12 13:37:13.717  INFO 14068 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-09-12 13:37:13.799  INFO 14068 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2020-09-12 13:37:13.837  INFO 14068 --- [           main] b.c.e.u.UndertowEmbeddedServletContainer : Undertow started on port(s) 8080 (http)
2020-09-12 13:37:13.840  INFO 14068 --- [           main] com.pangugle.testspringboot.App          : Started App in 1.638 seconds (JVM running for 1.875)
2020-09-12 13:37:31.240  INFO 14068 --- [  XNIO-2 task-1] io.undertow.servlet                      : Initializing Spring FrameworkServlet 'dispatcherServlet'
2020-09-12 13:37:31.240  INFO 14068 --- [  XNIO-2 task-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2020-09-12 13:37:31.246  INFO 14068 --- [  XNIO-2 task-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 6 ms

最后在浏览器访问:

http://127.0.0.1:8080/sayHello

然后就可以看到

hello spring boot!

至此 spring boot hello word 教程就完成了!是不是很简单呢?