1. 程式人生 > >spring-boot之hello word

spring-boot之hello word

spring boot環境的搭建,從spring官網下一個demo就可以了,地址:http://start.spring.io/。

1.pom.xml的配置(只需要加入一個spring boot啟動父依賴即可)

<?xml version="1.0" encoding="UTF-8"?>
<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>springboot</groupId> <artifactId>springboot-helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-helloworld :: HelloWorld Demo</name> <!-- Spring Boot 啟動父依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3
.3.RELEASE</version> </parent> <dependencies> <!-- Spring Boot web依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12
</version> </dependency> </dependencies> </project>

2.Controller層

package com.example.demo.controller;

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

@RestController
public class HelloWorldController {
    
    @RequestMapping("/")
    public String sayHello(){
        return "Hello,World!";
    }
    
}

@RestController和@RequestMapping註解是來自SpringMVC的註解,它們不是SpringBoot的特定部分。

1)@RestController:提供實現了REST API,可以服務JSON,XML或者其他。這裡是以String的形式渲染出結果。

2)@RequestMapping:提供路由資訊,"/“路徑的HTTP Request都會被對映到sayHello方法進行處理。

3.啟動應用類

開箱即用

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

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

 1)@SpringBootApplication:Spring Boot 應用的標識

 2)pplication很簡單,一個main函式作為主入口。SpringApplication引導應用,並將Application本身作為引數傳遞給run方法。具體run方法會啟動嵌入式的Tomcat並初始化Spring環境及其各Spring元件。

4.controller層測試類

一個好的程式,不能缺少好的UT。針對HelloWorldController的UT如下:

package com.example.controller;

import org.junit.Test;

import com.example.demo.controller.HelloWorldController;


public class HelloWorldControllerTest {
    
    @Test
    public void testSayHello(){
        System.out.println(new HelloWorldController().sayHello());
    }
    
}

5.執行

Just Run的宗旨,執行很簡單,直接右鍵Run執行Application類。同樣你也可以Debug Run。可以在控制檯中看到:

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

2018-11-19 22:05:34.642  INFO 11808 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on LAPTOP-8V3CCI0E with PID 11808 (started by 李玉龍 in D:\工作\work-springboot\demo)
2018-11-19 22:05:34.644  INFO 11808 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2018-11-19 22:05:35.578  INFO 11808 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-11-19 22:05:35.592  INFO 11808 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-11-19 22:05:35.592  INFO 11808 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.12
2018-11-19 22:05:35.600  INFO 11808 --- [           main] 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: [C:\Program Files\Java\jre1.8.0_101\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre1.8.0_101/bin/server;C:/Program Files/Java/jre1.8.0_101/bin;C:/Program Files/Java/jre1.8.0_101/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\System32\OpenSSH\;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\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Java\jdk1.8.0_101\bin;C:\Users\liyulong\AppData\Local\Microsoft\WindowsApps;;D:\工作\軟體\eclipse;;.]
2018-11-19 22:05:35.704  INFO 11808 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-11-19 22:05:35.704  INFO 11808 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1022 ms
2018-11-19 22:05:35.720  INFO 11808 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-11-19 22:05:35.722  INFO 11808 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-11-19 22:05:35.723  INFO 11808 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-11-19 22:05:35.723  INFO 11808 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'formContentFilter' to: [/*]
2018-11-19 22:05:35.723  INFO 11808 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-11-19 22:05:35.882  INFO 11808 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-11-19 22:05:36.020  INFO 11808 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-19 22:05:36.022  INFO 11808 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.611 seconds (JVM running for 1.999)
2018-11-19 22:05:40.145  INFO 11808 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-11-19 22:05:40.145  INFO 11808 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2018-11-19 22:05:40.149  INFO 11808 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms

然後訪問 http://localhost:8080/ ,即可在頁面中看到Spring Boot對你 say hello