eclipse如何創建spring boot工程:
第一步:首先打開eclipse,找到圖中的下圖的中“下三角”符號,選中"working sets"(表示將會把eclipse中的工程以working sets方式呈現,而不是單個project列表呈現)
第二步:依然是找出上圖的菜單,點擊“Select Working Set...”,彈出框:
點擊“New...”可以新建新的working set,也可以配置當前工作區域顯示哪些,不顯示哪些working set。
第三步:通過上邊的配置,我們新增一個springboot的working set,選中springboot該working set,然后新建maven工程:
SpringBoot工程創建操作步驟描述:
1)先選中springboot該working set;
2)在eclipse的導航欄中選中file->new->maven project;
3)在“New Maven Project”窗口中,勾選“Create a simple project(skip archetype selection)”
4)新建好的maven工程如下:
備注:
如果此時maven的pom.xml第一行出現錯誤:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
解決方案:
a、Help——>Install New SoftWare
b、http://repo1.maven.org/maven2/.m2e/connectors/m2eclipse-mavenarchiver/0.17.2/N/LATEST/
c、按照安裝提示,一直next,重啟eclipse之后,執行maven-update project。
5)在pom.xml中添加spring boot 2.0.1的依賴信息:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.dx</groupId> 5 <artifactId>springboot-helloword</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <!-- springboot父類依賴 --> 8 <parent> 9 <groupId>org.springframework.boot</groupId> 10 <artifactId>spring-boot-starter-parent</artifactId> 11 <version>2.0.1.RELEASE</version> 12 </parent> 13 14 <dependencies> 15 <!-- springboot web組件依賴 --> 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-web</artifactId> 19 </dependency> 20 </dependencies> 21 </project>
6)在src下新建com.dx.controller包,在包下創建HelloWordController.java
package com.dx.controller; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @EnableAutoConfiguration @Controller public class HelloWordController { @RequestMapping(value = "/index", method = RequestMethod.GET) public String index() { System.out.println("index is running..."); return "welcome"; } }
7)在com.dx.controller包下,創建MyApplication.java
import com.dx.controller.HelloWordController; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(HelloWordController.class, args); } }
此時,工程文件結構如下:
8)運行測試,在MyApplication.java編輯窗口中右鍵,run as -> Spring Boot App
運行后,訪問:http://localhost:8080/index
運行窗口打印信息如下:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.1.RELEASE) 2018-04-06 16:30:48.698 INFO 9376 --- [ main] com.dx.controller.MyApplication : Starting MyApplication on WIN-MHALQACGIOO with PID 9376 (D:\workspace5\springboot-helloword\target\classes started by Administrator in D:\workspace5\springboot-helloword) 2018-04-06 16:30:48.707 INFO 9376 --- [ main] com.dx.controller.MyApplication : No active profile set, falling back to default profiles: default 2018-04-06 16:30:48.813 INFO 9376 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@71c8becc: startup date [Fri Apr 06 16:30:48 CST 2018]; root of context hierarchy 2018-04-06 16:30:50.973 INFO 9376 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-04-06 16:30:51.013 INFO 9376 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-04-06 16:30:51.014 INFO 9376 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29 2018-04-06 16:30:51.032 INFO 9376 --- [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: [D:\Program Files\Java\jdk1.8.0_111\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/Program Files/Java/jdk1.8.0_111/bin/../jre/bin/server;D:/Program Files/Java/jdk1.8.0_111/bin/../jre/bin;D:/Program Files/Java/jdk1.8.0_111/bin/../jre/lib/amd64;C:\Python27;C:\Python27\Scripts;D:\Work\Java_Dev\mysql-5.7.17-winx64\bin;D:\Program Files\Java\jdk1.8.0_111\bin;D:\Program Files\Java\jdk1.8.0_111\jre\bin;C:\Program Files (x86)\scala\bin;%SBT_HOME%\bin;D:\演示\SuperMap\SMO_DotNET_810_14127_58257_Bin_x64\Bin_x64;%;ystemRoot%\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\VisualSVN Server\bin;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\IDM Computer Solutions\UltraEdit;D:\Work\Java\apache-maven-3.5.0-bin\apache-maven-3.5.0\bin;D:\RN_software\Git\cmd;D:\RN_software\nodejs\;D:\Program Files\android-sdk\platform-tools;D:\Program Files\android-sdk\tools;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;D:\ffmpeg-20170915-6743351-win64-static\bin;E:\Go\bin;C:\Program Files\Microsoft\Web Platform Installer\;D:\Program Files (x86)\Microsoft VS Code\bin;C:\Users\Administrator\AppData\Roaming\npm;C:\Users\Administrator\AppData\Local\GitHubDesktop\bin;D:\Program Files\eclipse-jee-mars-R-win32-x86_64\eclipse;;.] 2018-04-06 16:30:51.182 INFO 9376 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-04-06 16:30:51.182 INFO 9376 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2375 ms 2018-04-06 16:30:51.343 INFO 9376 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-04-06 16:30:51.348 INFO 9376 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-04-06 16:30:51.348 INFO 9376 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-04-06 16:30:51.349 INFO 9376 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-04-06 16:30:51.349 INFO 9376 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-04-06 16:30:51.464 INFO 9376 --- [ 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-06 16:30:51.761 INFO 9376 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@71c8becc: startup date [Fri Apr 06 16:30:48 CST 2018]; root of context hierarchy 2018-04-06 16:30:51.846 INFO 9376 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index],methods=[GET]}" onto public java.lang.String com.dx.controller.HelloWordController.index() 2018-04-06 16:30:51.851 INFO 9376 --- [ 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-06 16:30:51.852 INFO 9376 --- [ 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-06 16:30:51.878 INFO 9376 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-06 16:30:51.879 INFO 9376 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-04-06 16:30:52.073 INFO 9376 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-04-06 16:30:52.115 INFO 9376 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-04-06 16:30:52.120 INFO 9376 --- [ main] com.dx.controller.MyApplication : Started MyApplication in 4.223 seconds (JVM running for 7.319) 2018-04-06 16:31:02.889 INFO 9376 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2018-04-06 16:31:02.890 INFO 9376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2018-04-06 16:31:02.907 INFO 9376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 17 ms runing...
idea創建springboot工程:
1)新建一個空的工程:
2)新建空工程完成后,打開“Project Structure...”面板,新建maven工程
新建一個Module,把該Module作為一個maven工程來創建:
選中maven工程,來創建一個maven工程:
一步一步完成,把工程名字定義為springboot_helloword:
3)修改maven工程的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.dx</groupId> <artifactId>springboot-helloword</artifactId> <version>1.0-SNAPSHOT</version> <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> </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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4)新建完成的maven工程添加類,資源文件
a)新建com.dx.controller.HelloWordController.java、MyApplication.java
1 package com.dx.controller; 2 3 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 8 @EnableAutoConfiguration 9 @Controller 10 public class HelloWordController { 11 @RequestMapping(value = "/index", method = RequestMethod.GET) 12 public String index() { 13 System.out.println("index is running..."); 14 return "welcome"; 15 } 16 }
1 import com.dx.controller.HelloWordController; 2 import org.springframework.boot.SpringApplication; 3 import org.springframework.boot.autoconfigure.SpringBootApplication; 4 5 @SpringBootApplication 6 public class MyApplication { 7 public static void main(String[] args) { 8 SpringApplication.run(HelloWordController.class, args); 9 } 10 }
b)在資源文件夾下添加配置文件application.properties
#修改tomcat的默認的端口號,將8080改為8888
server.port=8888
c)添加css配置test.css:
body { padding: 0px; margin: auto; font-family: "黑體", "仿宋", Arial, "Arial Unicode MS", System; background-color: #00F; font-size: 20px; text-align: left; }
d)添加html靜態模版文件welcome.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <link href="css/test.css" rel="stylesheet"/> <body> <p>welcome page is login.........</p> </body> </html>
5)運行測試:
瀏覽器中輸入:http://127.0.0.1:8888/index