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

2019-07-14  本文已影响0人  togeek

一、目的

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

二、工具及环境

JDK:1.8.0_172
Eclipse:4.11
Maven:3.3.9

三、流程

1. 创建Maven工程

2. 添加代码

<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>cn.tvery</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>demo</name>
  <url>http://maven.apache.org</url>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
   </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </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>
  </dependencies>
</project>

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


6.png

弹出窗口,直接点击 OK


7..png
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

四、完整代码下载

下载

上一篇下一篇

猜你喜欢

热点阅读