IntelliJ IDEA 创建 Spring Boot 工程

本文介绍了 IntelliJ IDEA (Version 2017.3.3) 创建 Spring Boot 工程的过程。

1 创建新项目,在 IntelliJ IDEA 欢迎界面点击 Create New Project

2 在 New Project 窗口选中 Spring Initializr,设置 Project SDK,选择默认的 Spring Initializr 服务地址(Spring Boot 官方地址:https://start.spring.io),点击 Next

3 输入 Project Metadata 相关信息,注意 Artifact 中不能输入大写字母,点击 Next

4 选择需要添加的依赖,此处只选择 Web,点击 Next

5 输入 Project Name 和 Project Location,暂时不关心 More Settings 中内容,点击 Finish

6 IDEA 自动生成的目录结构

7 使用 Maven 管理依赖,自动生成的 POM 文件内容

<?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>learn.idea</groupId>
    <artifactId>idea_spring_boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.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>

8 src/main/resource 目录下生成的 application.properties 是 Spring Boot 的配置文件(也可以修改为 .yml 格式),初始化此文件无任何内容,需要开发人员自己配置,有关 Spring Boot 的配置不在本文讨论范围内。

9 IdeaSpringBootApplication.java 中包含应用入口,内容如下

package learn.idea;

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

@SpringBootApplication
public class IdeaSpringBootApplication {

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

10 自定义一个 Controller 处理首页 HTTP 请求(Spring MVC 知识)

package learn.idea.controller;

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

@RestController
public class IndexController {
    
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "这是 Spring Boot Web 首页";
    }
}

11 运行 IdeaSpringBootApplication 中的 main 方法启动工程,从启动日志中可以看出,Spring Boot Web 使用内嵌的 Tomcat 服务器

/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=49701 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1 -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49703:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/tools.jar:/Users/jiyongliang/Dev/Project/idea_spring_boot/target/classes:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/boot/spring-boot-starter-web/2.0.1.RELEASE/spring-boot-starter-web-2.0.1.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/boot/spring-boot-starter/2.0.1.RELEASE/spring-boot-starter-2.0.1.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/boot/spring-boot/2.0.1.RELEASE/spring-boot-2.0.1.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/boot/spring-boot-autoconfigure/2.0.1.RELEASE/spring-boot-autoconfigure-2.0.1.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/boot/spring-boot-starter-logging/2.0.1.RELEASE/spring-boot-starter-logging-2.0.1.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/apache/logging/log4j/log4j-to-slf4j/2.10.0/log4j-to-slf4j-2.10.0.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/apache/logging/log4j/log4j-api/2.10.0/log4j-api-2.10.0.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/yaml/snakeyaml/1.19/snakeyaml-1.19.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/boot/spring-boot-starter-json/2.0.1.RELEASE/spring-boot-starter-json-2.0.1.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/com/fasterxml/jackson/core/jackson-databind/2.9.5/jackson-databind-2.9.5.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/com/fasterxml/jackson/core/jackson-core/2.9.5/jackson-core-2.9.5.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.9.5/jackson-datatype-jdk8-2.9.5.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.9.5/jackson-datatype-jsr310-2.9.5.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/com/fasterxml/jackson/module/jackson-module-parameter-names/2.9.5/jackson-module-parameter-names-2.9.5.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/boot/spring-boot-starter-tomcat/2.0.1.RELEASE/spring-boot-starter-tomcat-2.0.1.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/apache/tomcat/embed/tomcat-embed-core/8.5.29/tomcat-embed-core-8.5.29.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/apache/tomcat/embed/tomcat-embed-el/8.5.29/tomcat-embed-el-8.5.29.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/apache/tomcat/embed/tomcat-embed-websocket/8.5.29/tomcat-embed-websocket-8.5.29.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/hibernate/validator/hibernate-validator/6.0.9.Final/hibernate-validator-6.0.9.Final.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/javax/validation/validation-api/2.0.1.Final/validation-api-2.0.1.Final.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/jboss/logging/jboss-logging/3.3.2.Final/jboss-logging-3.3.2.Final.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/com/fasterxml/classmate/1.3.4/classmate-1.3.4.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/spring-web/5.0.5.RELEASE/spring-web-5.0.5.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/spring-beans/5.0.5.RELEASE/spring-beans-5.0.5.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/spring-webmvc/5.0.5.RELEASE/spring-webmvc-5.0.5.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/spring-aop/5.0.5.RELEASE/spring-aop-5.0.5.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/spring-context/5.0.5.RELEASE/spring-context-5.0.5.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/spring-expression/5.0.5.RELEASE/spring-expression-5.0.5.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/spring-core/5.0.5.RELEASE/spring-core-5.0.5.RELEASE.jar:/Users/jiyongliang/Dev/Workbench/Maven/repo/org/springframework/spring-jcl/5.0.5.RELEASE/spring-jcl-5.0.5.RELEASE.jar learn.idea.IdeaSpringBootApplication

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

2018-04-18 20:45:09.878  INFO 695 --- [           main] learn.idea.IdeaSpringBootApplication     : Starting IdeaSpringBootApplication on jiyongliangdeiMac.local with PID 695 (/Users/jiyongliang/Dev/Project/idea_spring_boot/target/classes started by jiyongliang in /Users/jiyongliang/Dev/Project/idea_spring_boot)
2018-04-18 20:45:09.881  INFO 695 --- [           main] learn.idea.IdeaSpringBootApplication     : No active profile set, falling back to default profiles: default
2018-04-18 20:45:11.146  INFO 695 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@78dd667e: startup date [Wed Apr 18 20:45:11 CST 2018]; root of context hierarchy
2018-04-18 20:45:14.005  INFO 695 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-04-18 20:45:14.061  INFO 695 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-18 20:45:14.061  INFO 695 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-18 20:45:14.072  INFO 695 --- [ost-startStop-1] 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: [/Users/jiyongliang/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2018-04-18 20:45:14.269  INFO 695 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-18 20:45:14.270  INFO 695 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3169 ms
2018-04-18 20:45:14.433  INFO 695 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-04-18 20:45:14.436  INFO 695 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-18 20:45:14.437  INFO 695 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-04-18 20:45:14.437  INFO 695 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-04-18 20:45:14.437  INFO 695 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-04-18 20:45:14.631  INFO 695 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 20:45:14.964  INFO 695 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@78dd667e: startup date [Wed Apr 18 20:45:11 CST 2018]; root of context hierarchy
2018-04-18 20:45:15.030  INFO 695 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String learn.idea.controller.IndexController.index()
2018-04-18 20:45:15.035  INFO 695 --- [           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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-18 20:45:15.036  INFO 695 --- [           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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-18 20:45:15.053  INFO 695 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 20:45:15.054  INFO 695 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 20:45:15.234  INFO 695 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-18 20:45:15.375  INFO 695 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-04-18 20:45:15.379  INFO 695 --- [           main] learn.idea.IdeaSpringBootApplication     : Started IdeaSpringBootApplication in 7.995 seconds (JVM running for 13.031)

12 打开浏览器,输入 http://localhost:8080/

推荐阅读更多精彩内容