eclipse中创建springboot项目(maven)

曾经终败给现在 2022-05-19 01:11 476阅读 0赞
  1. 讲述一下,关于创建简单的springboot项目,使用maven方式。下篇会说下使用spring插件(sts)方式,以及二者的区别,学习摸索阶段,欢迎大家指正。
  2. **首先,在eclipse中创建一个maven项目**,File>>new>>Maven Project

70

如果没有搜索到maven project,说明你的eclipse中没有安装maven,需要在eclipse中配置下maven。不清楚的话可以查阅关于eclipse配置maven的相关资料。然后选择webapp点next,

70 1

然后填写项目名称和包名,点击finish即可。

70 2

  1. **修改pom.xml文件**,修改后的pom.xml文件为以下代码。
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.test.springboot</groupId>
  7. <artifactId>springboot-apply</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <name>springboot-apply</name>
  10. <url>http://www.example.com</url>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>1.5.2.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. </dependency>
  32. <dependency>
  33. <groupId>junit</groupId>
  34. <artifactId>junit</artifactId>
  35. <version>4.11</version>
  36. <scope>test</scope>
  37. </dependency>
  38. </dependencies>
  39. <build>
  40. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </pluginManagement>
  48. </build>
  49. </project>

添加两个Java类:一个SpringBootApplicationFirst,为SpringBoot程序的入口类

  1. package com.test.springboot.springboot_first;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * Hello world!
  6. *
  7. */
  8. @SpringBootApplication
  9. public class SpringBootApplicationFirst {
  10. public static void main(String[] args) {
  11. SpringApplication.run(SpringBootApplicationFirst.class, args);
  12. }
  13. }

一个Controller,用于页面返回结果。

  1. package com.test.springboot.springboot_first;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class TestController {
  6. @RequestMapping("test")
  7. public String test(){
  8. return "hello!";
  9. }
  10. }

之后在SpringBootApplicationFirst类的eclipse界面中,右键Run As >> Java Application ,运行即可

项目结构目录截图如下

70 3

运行之后,在console窗口,就能看到启动的springboot程序。

70 4

浏览器页面访问,即可看到访问成功,返回controller的文字。

70 5

发表评论

表情:
评论列表 (有 0 条评论,476人围观)

还没有评论,来说两句吧...

相关阅读