Spring基础 快速入门spring boot SPRING INITIALIZR

Posted firsttry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring基础 快速入门spring boot SPRING INITIALIZR相关的知识,希望对你有一定的参考价值。

SPRING INITIALIZR是spring boot在构建项目时候非常有效, 虽然说Maven以及Spring boot提供的starter使用起来非常简单, 但是由于组件和关联部分众多,有这样一个可视化的配置构建管理工具仍然很不错. 在这篇文章中我们将会在IntelliJ IDEA中利用SPRING INITIALIZR来创建一个Hello World的例子程序, 不包括Maven下载的速度的话, 1分钟应该够了.

技术分享图片

创建一个SPRING INITIALIZR项目

使用IntelliJ IDEA创建一个SPRING INITIALIZR类型的项目,除了选择web设定之外,一切全部Default。
技术分享图片

选择Web
技术分享图片
如果没有IntelliJ使用STS创建Starter类型的也是一样方便。二者都没有可以使用如下连接生成一个初始的项目zip包都是可以的。

技术分享图片

项目详细

创建之后安静地等待Maven把所有的依赖全部下载下来。抽空看一下创建的内容。
技术分享图片
可以看到,右侧代码区加亮部分正是SpringBoot的入口调用。
而左侧下方则是Maven辛辛苦苦替我们下载的内容。

Pom文件

Pom.xml文件如下所示

<?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>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

可以在的pendency中清楚地看到我们加入的Web部分。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4

修改DemoApplication

加入如下两行内容

    @RequestMapping("/")
    public String Hello(){
        return "Hello, Spring Boot...";
    }
  • 1
  • 2
  • 3
  • 4

同时对DemoApplication加上RestController注解,修改后的source如下

package com.example;

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

@RestController
@SpringBootApplication
public class DemoApplication {
    @RequestMapping("/")
    public String Hello(){
        return "Hello, Spring Boot...";
    }
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

运行

右键运行即可
技术分享图片

结果确认

运行起来后就可以在浏览器中查看结果了
技术分享图片

Console输出


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

2016-12-03 08:57:58.888  INFO 5128 --- [           main] com.example.DemoApplication              : Starting DemoApplication on vcc-PC with PID 5128 (C:UsersAdministratorIdeaProjectsdemo	argetclasses started by Administrator in C:UsersAdministratorIdeaProjectsdemo)
2016-12-03 08:57:58.891  INFO 5128 --- [           main] com.example.DemoApplication              : No active profile set, falling back to default profiles: default
2016-12-03 08:57:58.949  INFO 5128 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]30ee2816: startup date [Sat Dec 03 08:57:58 CST 2016]; root of context hierarchy
2016-12-03 08:58:00.512  INFO 5128 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-12-03 08:58:00.525  INFO 5128 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-12-03 08:58:00.526  INFO 5128 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6
2016-12-03 08:58:00.624  INFO 5128 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-12-03 08:58:00.624  INFO 5128 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1678 ms
2016-12-03 08:58:00.740  INFO 5128 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: ‘dispatcherServlet‘ to [/]
2016-12-03 08:58:00.744  INFO 5128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘characterEncodingFilter‘ to: [/*]
2016-12-03 08:58:00.744  INFO 5128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*]
2016-12-03 08:58:00.744  INFO 5128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘httpPutFormContentFilter‘ to: [/*]
2016-12-03 08:58:00.744  INFO 5128 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: ‘requestContextFilter‘ to: [/*]
2016-12-03 08:58:00.962  INFO 5128 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]30ee2816: startup date [Sat Dec 03 08:57:58 CST 2016]; root of context hierarchy
2016-12-03 08:58:01.018  INFO 5128 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.example.DemoApplication.Hello()
2016-12-03 08:58:01.022  INFO 5128 --- [           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)
2016-12-03 08:58:01.022  INFO 5128 --- [           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)
2016-12-03 08:58:01.043  INFO 5128 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-03 08:58:01.043  INFO 5128 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-03 08:58:01.071  INFO 5128 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-12-03 08:58:01.174  INFO 5128 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-12-03 08:58:01.226  INFO 5128 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-12-03 08:58:01.230  INFO 5128 --- [           main] com.example.DemoApplication              : Started DemoApplication in 2.751 seconds (JVM running for 3.109)
2016-12-03 08:58:06.041  INFO 5128 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet ‘dispatcherServlet‘
2016-12-03 08:58:06.041  INFO 5128 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet ‘dispatcherServlet‘: initialization started
2016-12-03 08:58:06.052  INFO 5128 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet ‘dispatcherServlet‘: initialization completed in 11 ms
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net









以上是关于Spring基础 快速入门spring boot SPRING INITIALIZR的主要内容,如果未能解决你的问题,请参考以下文章

spring boot学习总结-- 基础入门 Hello,spring boot!

Spring Boot 2.x基础教程:快速入门

Spring Cloud Spring Boot mybatis分布式微服务云架构快速入门

Spring Cloud Spring Boot mybatis分布式微服务云架构快速入门

Spring Cloud Spring Boot mybatis分布式微服务云架构快速入门

Spring Cloud Spring Boot mybatis分布式微服务云架构快速入门