SpringBoot(一):使用eclipse/idea创建springboot helloword工程

eclipse如何创建spring boot工程:

第一步:首先打开eclipse,找到图中的下图的中“下三角”符号,选中"working sets"(表示将会把eclipse中的工程以working sets方式呈现,而不是单个project列表呈现)

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

第二步:依然是找出上图的菜单,点击“Select Working Set...”,弹出框:

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

点击“New...”可以新建新的working set,也可以配置当前工作区域显示哪些,不显示哪些working set。

第三步:通过上边的配置,我们新增一个springboot的working set,选中springboot该working set,然后新建maven工程:

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

SpringBoot工程创建操作步骤描述:

1)先选中springboot该working set;

2)在eclipse的导航栏中选中file->new->maven project;

3)在“New Maven Project”窗口中,勾选“Create a simple project(skip archetype selection)”

4)新建好的maven工程如下:

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

备注:

如果此时maven的pom.xml第一行出现错误:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)

解决方案:

a、Help——>Install New SoftWare

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

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的依赖信息:

 <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>0.0.1-SNAPSHOT</version>
<!-- springboot父类依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent> <dependencies>
<!-- springboot web组件依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</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);
}
}

此时,工程文件结构如下:

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

8)运行测试,在MyApplication.java编辑窗口中右键,run as -> Spring Boot App

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

运行后,访问: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)新建一个空的工程:

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

2)新建空工程完成后,打开“Project Structure...”面板,新建maven工程

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

新建一个Module,把该Module作为一个maven工程来创建:

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

选中maven工程,来创建一个maven工程:

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

一步一步完成,把工程名字定义为springboot_helloword:

SpringBoot(一):使用eclipse/idea创建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工程添加类,资源文件

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

a)新建com.dx.controller.HelloWordController.java、MyApplication.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";
}
}
 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);
}
}

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)运行测试:

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

浏览器中输入:http://127.0.0.1:8888/index

SpringBoot(一):使用eclipse/idea创建springboot helloword工程

上一篇:eclipse的安装


下一篇:(三)Installation