xytBK

SpringBoot学习 (一) Eclipse中创建新的SpringBoot项目

1. Eclipse中安装STS插件

 (1)在线安装

  • Help--Eclipse Marketplace...
  • 搜索“STS”,点击“install”安装
 
 (2)本地安装
  •  打开网页 http://spring.io/tools/sts/all
  •  下载适合自己的eclipse版本的STS压缩包
  • 下载后,在eclipse操作: Help--Install New Software--Add--Archive,添加已下载的zip包

  • 安装有Spring IDE字样的插件即可,取消勾选自动更新,接下来就有Next就点Next,有Finish就点Finish

 

2. 新建Maven项目
  •   File--New--Project...
  •   Maven--Maven Project--Next--Next
  • 选择maven-archetype-webapp后,Next
  • 输入项目名和包名后,Finish,完成创建
 
3. 编写显示“Hello World”的程序
  • 编辑pom.xml文件,添加SpringBoot的依赖

 

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.project</groupId>
 7     <artifactId>website</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9 
10     <!-- Inherit defaults from Spring Boot -->
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>1.4.0.RELEASE</version>
15     </parent>
16 
17     <!-- Add typical dependencies for a web application -->
18     <dependencies>
19       <dependency>
20         <groupId>org.springframework.boot</groupId>
21         <artifactId>spring-boot-starter-web</artifactId>
22       </dependency>
23       <dependency>
24         <groupId>org.springframework.boot</groupId>
25         <artifactId>spring-boot-starter-test</artifactId>
26         <scope>test</scope>
27       </dependency>
28     </dependencies>
29 
30     <!-- Package as an executable jar -->
31     <build>
32         <plugins>
33             <plugin>
34                 <groupId>org.springframework.boot</groupId>
35                 <artifactId>spring-boot-maven-plugin</artifactId>
36             </plugin>
37         </plugins>
38     </build>
39 
40 </project>
复制代码
  • 保存XML后Maven开始下载相关的包(ps:国内不FQ的话可能老是下载失败,建议改用国内的镜像)
  • 在如下路径创建一个主类

复制代码
 1 package com.website;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 
 9 @SpringBootApplication
10 @Controller
11 public class WebsiteApplication {
12     @RequestMapping("/")
13     @ResponseBody
14     String index() {
15         return "Hello World";
16     }
17 
18     public static void main(String[] args) {
19         SpringApplication.run(WebsiteApplication.class, args);
20     }
21 }
复制代码
  • 右键单击该主类--Run As--Java Application
  • console启动界面如下图

  • 启动后,在浏览器输入 localhost:8080,将显示Hello World字样

 

此文章借鉴于https://www.cnblogs.com/kwony/p/7079779.html

分类:

技术点:

相关文章:

  • 2021-06-18
  • 2021-08-23
  • 2021-11-29
  • 2021-11-19
猜你喜欢
  • 2021-11-29
  • 2021-04-11
  • 2021-07-30
  • 2021-08-18
  • 2021-10-28
  • 2021-07-31
  • 2021-10-23
相关资源
相似解决方案