Spring boot入门:在Eclipse中搭建Spring boot 项目

 

Eclipse中的STS插件

    • 打开Eclipse-Help-Eclipse Marketplace-popular
    • Spring boot(6)---在Eclipse中搭建Spring boot 项目
    • 下载完成后,重启eclipse,选择新建project-spring-spring start project,新建Spring boot 项目。
    • Spring boot(6)---在Eclipse中搭建Spring boot 项目

项目快速解析

  • 新建的项目目录大概如下
    Spring boot(6)---在Eclipse中搭建Spring boot 项目
  • 整体目录结构和Spring项目一样,只不过Spring boot的配置文件迁移到了application.yml(或者application.propertis)。项目的入口为BootTestApplication.java的main函数入口。
  • 在maven配置的pom.xml里面相较于之前的项目添加了以下依赖:
    Spring boot(6)---在Eclipse中搭建Spring boot 项目 Spring boot(6)---在Eclipse中搭建Spring boot 项目
  • BootTestApplication中@SpringBootApplication取代了Spring项目中的@Configuration、@EnableAutoConfiguration、@ComponentScan。因此,我们所有的bean需要在BootTestApplication的同级目录下被扫描。
    Spring boot(6)---在Eclipse中搭建Spring boot 项目

注意:application的文件一定是要在controller或者其它文件的top一级,如图:

Spring boot(6)---在Eclipse中搭建Spring boot 项目

就是说Application.java文件和web文件夹是在同一个层级的,web文件夹底下才是controller的文件。

 


快速开发项目

  • 在pom.xml引入依赖
  • 1

    2

    3

    <dependency><groupId>org.springframework.boot</groupId

    <artifactId>spring-boot-starter-web</artifactId

    </dependency>

  • 在com.example.demo下新建controller包,新建controller包,在下面建立Hello Controller.java。
  • 1

    2

    3

    4

    5

    6

    7

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    @RestController 

    public class HelloController { 

    @RequestMapping(value="/hello", method=RequestMethod.GET) 

    public String HelloController() { 

    return "Hello World!"

    }

  • 右键BootTestApplication.java-run as java application,运行项目。
  • 在浏览器输入localhost:8080/hello。
  • Spring boot(6)---在Eclipse中搭建Spring boot 项目

修改访问端口和默认路径

  • 在这里我们使用更加好看的application.yml方式配置。只需要将默认的application.properties替换为application.yml即可。
  • 在yml添加以下代码,修改端口为8081,后缀添加路径为/arvin
  • 1

    2

    3

    server:

    port: 8081

    context-path: /arvin

  • 打开浏览器,输入http://localhost:8081/arvin/hello

相关文章:

  • 2021-11-22
  • 2021-12-22
  • 2019-10-25
  • 2022-03-04
  • 2021-09-17
  • 2022-12-23
猜你喜欢
  • 2021-10-19
  • 2021-06-01
  • 2021-07-30
  • 2021-11-14
  • 2021-07-16
  • 2021-07-11
相关资源
相似解决方案