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)Application很简单,一个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

 

posted @ 2018-11-19 22:24  crazyPants  阅读(258)  评论(0编辑  收藏  举报