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

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工程如下:

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.ser[email protected]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.ser[email protected]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

原文地址:https://www.cnblogs.com/yy3b2007com/p/8728182.html

时间: 2024-10-14 04:02:47

SpringBoot(一):使用eclipse/idea创建springboot helloword工程的相关文章

Eclipse中创建标准web工程以及标准目录结构说明

最近公司有个Web项目,项目结构如下: 虽然运行没有错,但是实在是别扭,标准的web应用一般不采用这种结构: 因此总结一下:     1.如何在Eclipse中创建一个标准的Web应用.     2.标准Web应用推荐的目录结构.   第一:如何在Eclipse中创建一个标准的web应用:(假设项目名称为:myweb) 1) PS:projectname一般全小写字母,最好不要出现数字和大写字母. 2) PS:default output flolder要根据project_name修改. 3)

Spring-boot(一)通过向导快速创建Spring-boot项目

通过向导快速创建Spring-boot项目 创建步骤: 选择Spring Initializr 填写组织和模块名 选择对应的模块 注:这里左侧的模块比较多,玩家可以根据自己的实际需要自由选择,此处暂时选择web 完成 等待项目自动下载相关依赖完成项目创建 注:项目类型为maven的话,需要配置阿里云仓库才不会特别慢 setting.xml <mirrors> <mirror> <id>alimaven</id> <name>aliyun mave

使用Eclipse快速创建父子聚合工程

1.创建maven父工程步骤:new-->other-->选择maven project-->next-->勾选create a simple project-->next-->填写GAV-->packaging选择pom-->finish.(一定要勾选使用默认的工作空间) 2.创建maven子工程步骤:选中刚才创建的父工程右键-->new-->other-->选择maven module-->next-->勾选create a

eclipse IED 创建springboot项目教程

学习使我沉淀变成淀粉.-- 沃兹基硕德[美] 首先给你的 eclipse 安装插件 Help> Eclipse Marketplace uploading-image-736738.png 所搜 sts 关键字 找打这个 安装 installed 安装插件导向窗口完成后,在eclipse右下角将会出现安装插件的进度,等插件安装完成后重启eclipse生效 [未写完] 原文地址:https://www.cnblogs.com/userzf/p/11024538.html

eclipse maven 创建总POM 工程

首先进入到eclipse的workspace,我这里的workspace目录是D:\workspace 1.创建总的POM D:\workspace>mvn archetype:create -DgroupId=com.scall -DartifactId=scall   创建一个maven项目,然后修改该项目的pom.xml文件,package类型改为pom <packaging>pom</packaging> 并且删除src目录 2.创建子模块     创建子模块1,在总

Eclipse中创建新的SpringBoot项目(打包并且部署到tomcat)

Spring-boot因为其对jar包的高度集成以及简化服务配置,快速部署等的优点,逐渐成为Java开发人员的热衷的框架.下面演示一下怎么在Eclipse中新建Spring-boot项目以及打包部署. 在Eclipse中创建Spring-boot有两种方式 方式一 进入Spring-boot的在线配置网站http://start.spring.io/:定制自己的项目服务组件,下载到本地并使用Eclipse导入项目 服务组件: Web:web项目组件,包含spring mvc,tomcat,log

在Eclipse中如何发布创建的JavaWeb工程

博客<在Eclipse中如何创建JavaWeb工程>中图文并茂的说明了Eclipse中创建JavaWeb工程的方法:博客<如何为Eclipse开发工具中创建的JavaWeb工程创建Servlet>则说明了如何为Eclipse内创建的JavaWeb工程创建Servlet:本篇博客将告诉大家如何发布在Eclipse开发工具中创建的JavaWeb工程: 1.为了更好地看到效果,进行如下操作: a).将博客<如何为Eclipse开发工具中创建的JavaWeb工程创建Servlet&g

eclipse创建springboot项目

如果eclipse没有安装STS插件是没有办法创建springboot项目的,也就没有如图这个目录. 下载STS插件步骤如下,点击help-->选择Eclipse Marketplace--->popular,如下. 安装需要等待一段时间,然后还需要选择同意安装之后才可以进行安装. 原文地址:https://www.cnblogs.com/ming-blogs/p/10289018.html

gradle创建spring-boot项目

刚来新公司,熟悉了公司项目搭建的框架,了解到了一种新的项目管理工具:gradle,从网上了解,据说比maven更加灵活化,于是便学习了一番.在此记录下来,一遍以后使用.gradle的安装就不说了,网上一抓一把.直接说创建spring-boot过程. 1.首先,初始化gradle项目 gradle init 初始化以后,会在项目下产生标准被gralde目录. . ├── build.gradle ├── gradle │   └── wrapper │   ├── gradle-wrapper.j