欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Boot |使用IDEA搭建第一个Spring Boot程序

程序员文章站 2024-03-13 12:32:03
...


第一个Spring Boot实现的功能:
浏览器发送hello请求,服务器接收请求并处理,响应"HelloWorld!"字符串。

环境准备

工具 版本
JDK 1.8
Apache Maven 3.5.4
IntelliJIDE 2019-2
SpringBoot 1.5.4

第一步:创建一个Maven程序

IDEA整合Maven
Spring Boot |使用IDEA搭建第一个Spring Boot程序
创建Maven程序
Spring Boot |使用IDEA搭建第一个Spring Boot程序

第二步:导入SpringBoot相关依赖

<?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.gql</groupId>
    <artifactId>springboot-01-helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

可以看到,此时已经自动导入了相关的jar包
Spring Boot |使用IDEA搭建第一个Spring Boot程序

第三步:编写主程序

  • @SpringBootApplication: 是一个复合注解,使用该注解告诉Springboot启用自动配置和组件扫描功能。
  • SpringApplication.run(HelloWorld.class, args); :将Spring应用启动起来。
package com.gql;

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

/**
 * Class description:
 * 		主程序中启动SpringBoot
 * @guoqianliang
 */
@SpringBootApplication
public class HelloWorld {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorld.class, args);
    }
}

第四步:编写Controller

  • @RequestMapping("/hello"):接收浏览器的hello请求。
  • @ResponseBody:将"Hello World!"写给浏览器
package com.gql.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Class description:
 * 		Controller控制器
 * @guoqianliang
 */
@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return "Hello World!";
    }
}

第五步:将程序跑起来

主程序运行Main方法后直接在浏览器访问(Springboot内置了tomcat容器):
Spring Boot |使用IDEA搭建第一个Spring Boot程序

项目工程结构如下:
Spring Boot |使用IDEA搭建第一个Spring Boot程序

第六步:简化部署

在pom中添加pom插件后,在Maven选项卡中双击package即可一键打包成jar,在dos中可以直接java -jar进行执行。

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

Spring Boot |使用IDEA搭建第一个Spring Boot程序
可以在控制台找到文件打包后放置的位置:

[INFO] Building jar: D:\Program Files\JAVA\workspace_idea\springboot01helloworld\target\springboot-01-helloworld-1.0-SNAPSHOT.jar

也可以在IDEA中直接看到自动生成的jar包
Spring Boot |使用IDEA搭建第一个Spring Boot程序


将此jar包copy到桌面,运行dos命令,执行jar包:

  • java -jar springboot-01-helloworld-1.0-SNAPSHOT.jar
Microsoft Windows [版本 10.0.18363.720]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\Hudie>C:\Users\Hudie\Desktop
'C:\Users\Hudie\Desktop' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

C:\Users\Hudie>cd C:\Users\Hudie\Desktop

C:\Users\Hudie\Desktop>java -jar springboot-01-helloworld-1.0-SNAPSHOT.jar

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

2020-03-21 12:10:53.952  INFO 12048 --- [           main] com.gql.HelloWorld                       : Starting HelloWorld v1.0-SNAPSHOT on LAPTOP-EPI7POB8 with PID 12048 (C:\Users\Hudie\Desktop\springboot-01-helloworld-1.0-SNAPSHOT.jar started by Hudie in C:\Users\Hudie\Desktop)
2020-03-21 12:10:53.956  INFO 12048 --- [           main] com.gql.HelloWorld                       : No active profile set, falling back to default profiles: default
2020-03-21 12:10:54.023  INFO 12048 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@30946e09: startup date [Sat Mar 21 12:10:54 CST 2020]; root of context hierarchy
2020-03-21 12:10:55.958  INFO 12048 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2020-03-21 12:10:56.006  INFO 12048 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-21 12:10:56.011  INFO 12048 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2020-03-21 12:10:56.271  INFO 12048 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-21 12:10:56.271  INFO 12048 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2250 ms
2020-03-21 12:10:56.514  INFO 12048 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2020-03-21 12:10:56.522  INFO 12048 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2020-03-21 12:10:56.523  INFO 12048 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-03-21 12:10:56.523  INFO 12048 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2020-03-21 12:10:56.523  INFO 12048 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2020-03-21 12:10:57.084  INFO 12048 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.bootaaa@qq.com30946e09: startup date [Sat Mar 21 12:10:54 CST 2020]; root of context hierarchy
2020-03-21 12:10:57.200  INFO 12048 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.gql.Controller.HelloController.hello()
2020-03-21 12:10:57.206  INFO 12048 --- [           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-03-21 12:10:57.206  INFO 12048 --- [           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-03-21 12:10:57.240  INFO 12048 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-03-21 12:10:57.240  INFO 12048 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-03-21 12:10:57.292  INFO 12048 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-03-21 12:10:57.518  INFO 12048 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2020-03-21 12:10:57.631  INFO 12048 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2020-03-21 12:10:57.637  INFO 12048 --- [           main] com.gql.HelloWorld                       : Started HelloWorld in 4.025 seconds (JVM running for 4.416)
2020-03-21 12:11:06.815  INFO 12048 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2020-03-21 12:11:06.815  INFO 12048 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2020-03-21 12:11:06.837  INFO 12048 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 20 ms

此时项目就启动了,可以在浏览器直接访问了。

Spring Boot |使用IDEA搭建第一个Spring Boot程序