(一)最简单的springBoot启动WEB工程

一、目的

简单的方式使用springboot启动WEB工程

二、工具及环境

JDK:1.8.0_172
Eclipse:4.11
Maven:3.3.9

三、流程

1. 创建Maven工程

  • 打开eclipse,并通过菜单创建Maven Project


    1.png
  • 点击Next


    2.png
  • 选择快速启动的模板原型,点击Next


    3.png
  • 填写Group Id和Artifact Id,点击 Finish


    4.png
  • Maven工程创建完毕,完成后的目录结构


    5.png

2. 添加代码

  • 修改pom.xml文件
    添加spring-boot-starter-parent,spring-boot-starter-web,spring-boot-starter-tes,删除junit块,完成后的代码如下

  4.0.0

  cn.tvery
  demo
  0.0.1-SNAPSHOT
  jar

  demo
  http://maven.apache.org
  
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
   
  
    UTF-8
  

  
    
            org.springframework.boot
            spring-boot-starter-web
    
    
         org.springframework.boot
         spring-boot-starter-test
         test
     
  


添加后保存,稍等片刻,等待maven下载jar包,然后Alt+F5,或者按照下图所示刷新下maven


6.png

弹出窗口,直接点击 OK


7..png
  • 添加类文件
    在cn.tvery.demo.controller包下创建HelloController.java
package cn.tvery.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("")
    public String hello() {
        return "hello world";
    }
}

在cn.tvery.demo包下创建SpringBootRun.java

package cn.tvery.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRun 
{
    public static void main( String[] args ){
        SpringApplication.run(SpringBootRun.class, args);
    }
}

3. 启动并测试

找到SpringBootRun.java的main方法,右键Run as=>java Application


8.png

启动后,控制台打印出如下信息,端口8080,上下文是空的


9.png

现在就可以在浏览器的地址栏上输入http:localhost:8080/hello进行访问
10.png

四、完整代码下载

下载

你可能感兴趣的:((一)最简单的springBoot启动WEB工程)