SpringBoot 2.x 学习笔记(1):入门实例(IntelliJ IDEA)

2022-05-06 20:44:46 浏览数 (1)

(1)创建新项目

(2)Spring Initializr

(3)输入项目元数据

(4)选择依赖包

(5)项目存放路径

(6)默认生成的pom.xml

代码语言:javascript复制
<?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>cn.hadron</groupId>
	<artifactId>springboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

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

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

注意,为了加快相关jar下载速度,可以在pom.xml中添加国内的阿里Maven镜像,在<properties>...</properties>元素后添加如下元素即可

代码语言:javascript复制
 <repositories>
    <repository>
      <id>nexus-aliyun</id>
      <name>Nexus aliyun</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </repository>
  </repositories>

(7)等待项目创建完成

(8)生成的默认SpringbootApplication

代码语言:javascript复制
package cn.hadron.springboot;

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

@SpringBootApplication
public class SpringbootApplication {

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

(9)新建控制器

代码语言:javascript复制
package cn.hadron.springboot.controller;
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 
@Controller 
public class HelloController {    
	@RequestMapping("/hello")    
	@ResponseBody    
	public String hello() {
	        return "Hello,Spring Boot 2.0 !";    
    } 
}
代码语言:javascript复制
package cn.hadron.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
@RestController 
public class HiController {    
	@RequestMapping("/hi")    
	public String hi() {
	        return "Hi,Spring Boot 2.0 !";    
    } 
}

(10)运行 单击绿色的三角按钮,运行程序

代码语言:javascript复制
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* "C:Program FilesJavajdk1.8.0_112binjava" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=50042 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2017.2.5libidea_rt.jar=50043:C:Program FilesJetBrainsIntelliJ IDEA 2017.2.5bin" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_112jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_112jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_112jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_112jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_112jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_112jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_112jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_112jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_112jrelibextnashorn.jar;C:Program FilesJavajdk1.8.0_112jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_112jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_112jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_112jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_112jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_112jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_112jrelibjce.jar;C:Program FilesJavajdk1.8.0_112jrelibjfr.jar;C:Program FilesJavajdk1.8.0_112jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_112jrelibjsse.jar;C:Program FilesJavajdk1.8.0_112jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_112jrelibplugin.jar;C:Program FilesJavajdk1.8.0_112jrelibresources.jar;C:Program FilesJavajdk1.8.0_112jrelibrt.jar;D:IdeaProjectsspringboottargetclasses;C:UsersAdministrator.m2repositoryorgspringframeworkbootspring-boot-starter-web2.0.3.RELEASEspring-boot-starter-web-2.0.3.RELEASE.jar;C:UsersAdministrator.m2repositoryorgspringframeworkbootspring-boot-starter2.0.3.RELEASEspring-boot-starter-2.0.3.RELEASE.jar;C:UsersAdministrator.m2repositoryorgspringframeworkbootspring-boot2.0.3.RELEASEspring-boot-2.0.3.RELEASE.jar;C:UsersAdministrator.m2repositoryorgspringframeworkbootspring-boot-autoconfigure2.0.3.RELEASEspring-boot-autoconfigure-2.0.3.RELEASE.jar;C:UsersAdministrator.m2repositoryorgspringframeworkbootspring-boot-starter-logging2.0.3.RELEASEspring-boot-starter-logging-2.0.3.RELEASE.jar;C:UsersAdministrator.m2repositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;C:UsersAdministrator.m2repositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;C:UsersAdministrator.m2repositoryorgapachelogginglog4jlog4j-to-slf4j2.10.0log4j-to-slf4j-2.10.0.jar;C:UsersAdministrator.m2repositoryorgapachelogginglog4jlog4j-api2.10.0log4j-api-2.10.0.jar;C:UsersAdministrator.m2repositoryorgslf4jjul-to-slf4j1.7.25jul-to-slf4j-1.7.25.jar;C:UsersAdministrator.m2repositoryjavaxannotationjavax.annotation-api1.3.2javax.annotation-api-1.3.2.jar;C:UsersAdministrator.m2repositoryorgyamlsnakeyaml1.19snakeyaml-1.19.jar;C:UsersAdministrator.m2repositoryorgspringframeworkbootspring-boot-starter-json2.0.3.RELEASEspring-boot-starter-json-2.0.3.RELEASE.jar;C:UsersAdministrator.m2repositorycomfasterxmljacksoncorejackson-databind2.9.6jackson-databind-2.9.6.jar;C:UsersAdministrator.m2repositorycomfasterxmljacksoncorejackson-annotations2.9.0jackson-annotations-2.9.0.jar;C:UsersAdministrator.m2repositorycomfasterxmljacksoncorejackson-core2.9.6jackson-core-2.9.6.jar;C:UsersAdministrator.m2repositorycomfasterxmljacksondatatypejackson-datatype-jdk82.9.6jackson-datatype-jdk8-2.9.6.jar;C:UsersAdministrator.m2repositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.9.6jackson-datatype-jsr310-2.9.6.jar;C:UsersAdministrator.m2repositorycomfasterxmljacksonmodulejackson-module-parameter-names2.9.6jackson-module-parameter-names-2.9.6.jar;C:UsersAdministrator.m2repositoryorgspringframeworkbootspring-boot-starter-tomcat2.0.3.RELEASEspring-boot-starter-tomcat-2.0.3.RELEASE.jar;C:UsersAdministrator.m2repositoryorgapachetomcatembedtomcat-embed-core8.5.31tomcat-embed-core-8.5.31.jar;C:UsersAdministrator.m2repositoryorgapachetomcatembedtomcat-embed-el8.5.31tomcat-embed-el-8.5.31.jar;C:UsersAdministrator.m2repositoryorgapachetomcatembedtomcat-embed-websocket8.5.31tomcat-embed-websocket-8.5.31.jar;C:UsersAdministrator.m2repositoryorghibernatevalidatorhibernate-validator6.0.10.Finalhibernate-validator-6.0.10.Final.jar;C:UsersAdministrator.m2repositoryjavaxvalidationvalidation-api2.0.1.Finalvalidation-api-2.0.1.Final.jar;C:UsersAdministrator.m2repositoryorgjbossloggingjboss-logging3.3.2.Finaljboss-logging-3.3.2.Final.jar;C:UsersAdministrator.m2repositorycomfasterxmlclassmate1.3.4classmate-1.3.4.jar;C:UsersAdministrator.m2repositoryorgspringframeworkspring-web5.0.7.RELEASEspring-web-5.0.7.RELEASE.jar;C:UsersAdministrator.m2repositoryorgspringframeworkspring-beans5.0.7.RELEASEspring-beans-5.0.7.RELEASE.jar;C:UsersAdministrator.m2repositoryorgspringframeworkspring-webmvc5.0.7.RELEASEspring-webmvc-5.0.7.RELEASE.jar;C:UsersAdministrator.m2repositoryorgspringframeworkspring-aop5.0.7.RELEASEspring-aop-5.0.7.RELEASE.jar;C:UsersAdministrator.m2repositoryorgspringframeworkspring-context5.0.7.RELEASEspring-context-5.0.7.RELEASE.jar;C:UsersAdministrator.m2repositoryorgspringframeworkspring-expression5.0.7.RELEASEspring-expression-5.0.7.RELEASE.jar;C:UsersAdministrator.m2repositoryorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar;C:UsersAdministrator.m2repositoryorgspringframeworkspring-core5.0.7.RELEASEspring-core-5.0.7.RELEASE.jar;C:UsersAdministrator.m2repositoryorgspringframeworkspring-jcl5.0.7.RELEASEspring-jcl-5.0.7.RELEASE.jar" cn.hadron.springboot.SpringbootApplication
*/

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

2018-07-17 23:29:19.749  INFO 2388 --- [           main] c.h.springboot.SpringbootApplication     : Starting SpringbootApplication on Win7 with PID 2388 (D:IdeaProjectsspringboottargetclasses started by Administrator in D:IdeaProjectsspringboot)
2018-07-17 23:29:19.765  INFO 2388 --- [           main] c.h.springboot.SpringbootApplication     : No active profile set, falling back to default profiles: default
2018-07-17 23:29:19.812  INFO 2388 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2145433b: startup date [Tue Jul 17 23:29:19 CST 2018]; root of context hierarchy
2018-07-17 23:29:21.278  INFO 2388 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-07-17 23:29:21.325  INFO 2388 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-07-17 23:29:21.325  INFO 2388 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-07-17 23:29:21.340  INFO 2388 --- [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: [C:Program FilesJavajdk1.8.0_112bin;C:WindowsSunJavabin;C:Windowssystem32;C:Windows;C:Program FilesJavajdk1.8.0_112bin;C:Program Files (x86)Common FilesNetSarang;C:ProgramDataOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program FilesGitcmd;D:hadoop-2.7.3bin;D:apache-maven-3.5.0bin;C:Program Files (x86)Common FilesThunder NetworkKanKanCodecs;C:UsersAdministratorAppDataLocalProgramsPythonPython35-32Scripts;C:UsersAdministratorAppDataLocalProgramsPythonPython35-32;.]
2018-07-17 23:29:21.530  INFO 2388 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-07-17 23:29:21.530  INFO 2388 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1718 ms
2018-07-17 23:29:21.670  INFO 2388 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-07-17 23:29:21.670  INFO 2388 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-17 23:29:21.670  INFO 2388 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-17 23:29:21.670  INFO 2388 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-17 23:29:21.670  INFO 2388 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-17 23:29:21.810  INFO 2388 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-17 23:29:22.029  INFO 2388 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2145433b: startup date [Tue Jul 17 23:29:19 CST 2018]; root of context hierarchy
2018-07-17 23:29:22.076  INFO 2388 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String cn.hadron.springboot.controller.HelloController.hello()
2018-07-17 23:29:22.076  INFO 2388 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hi]}" onto public java.lang.String cn.hadron.springboot.controller.HiController.hi()
2018-07-17 23:29:22.093  INFO 2388 --- [           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-07-17 23:29:22.093  INFO 2388 --- [           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-07-17 23:29:22.109  INFO 2388 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-17 23:29:22.109  INFO 2388 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-07-17 23:29:22.280  INFO 2388 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-07-17 23:29:22.358  INFO 2388 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-07-17 23:29:22.374  INFO 2388 --- [           main] c.h.springboot.SpringbootApplication     : Started SpringbootApplication in 3.202 seconds (JVM running for 6.423)

0 人点赞