美文网首页
(一)最简单的springBoot启动WEB工程

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

作者: togeek | 来源:发表于2019-07-14 20:42 被阅读0次

    一、目的

    简单的方式使用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块,完成后的代码如下
    <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
    • 添加类文件
      在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工程

          本文链接:https://www.haomeiwen.com/subject/tmrikctx.html