zoukankan      html  css  js  c++  java
  • springboot的helloworld

    Spring Boot 它的设计目的是简化开发的,不需要写各种配置文件,引入相关的依赖就能快速搭建一个工程。换句话说,**使用springboot后,编码,配置, 部署和监控都变得更简单!**

    接下来我们写个HelloWord吧!

    先说下我的环境:jdk 1.8maven 3.5.3  eclipse

    打开 eclipse -> new -> Maven Project -> 勾上web(开启web功能)-> 填写groupartifact -> 点下一步就行了。

    可以看到目录结构有如下几个:

    1/src/main/java/  存放项目所有源代码目录

    2/src//main/resources/  存放项目所有资源文件以及配置文件目录

    3/src/test/  存放测试代码目录

    <br/>

    1,修改 pom 文件

    ```

    <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>com.yideng.springboot</groupId>

    <artifactId>my-spring-boot</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <parent>

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

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

      <version>2.0.3.RELEASE</version>

      <relativePath/>

    </parent>

    <dependencies>

      <dependency>

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

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

      </dependency>

    </dependencies>

    </project>

    ```

    spring-boot-starter:核心模块,包括自动配置支持、日志和YAML

    spring-boot-starter-test:测试模块,包括JUnitHamcrest

    2, 创建 Java 代码

    ```

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication

    public class DemoApplication {

      public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

      }

    }

    ```

    3, 引入web模块,修改pom.xml,添加支持web的依赖:

    ```

    <dependency>

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

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

     </dependency>

    ```

    4, 编写 controller

    ```

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

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

    @RestController

    public class HelloController001 {

      @RequestMapping("/hello")

      public String sayHello() {

        return "hello ,spring boot";

      }

    }

    ```

    5, 启动主程序 DemoApplication

    浏览器访问   **http://localhost:8080/hello**  

    可以看到页面上显示hello ,spring boot”。

    这里,我们再写个 controller

    ```

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

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

    @RestController

    @RequestMapping("/test")

    public class HelloController002 {

      @RequestMapping("/hi")

      public String sayHi() {

        return "你好 ,spring boot, ";

      }

    }

    ```

    接着再次启动主程序 DemoApplication , 浏览器访问 **http://localhost:8080/test/hi**

    可以看到页面上显示“你好 ,spring boot, ”。

    可以发现,spring boot已经启动

    1, 嵌入的Tomcat,无需部署WAR文件,默认端口号为8080

    2, 默认我们的项目命名空间为"/"

    spring boothelloworld就到此了。

  • 相关阅读:
    AD域渗透测试笔记
    ctf之WEB练习一
    CTF之crpto练习三
    ctf之WEB练习二
    ctf之WEB练习三
    [转]Ant学习笔记——自己构建Ant编译环境
    [转]【NoSQL】NoSQL入门级资料整理(CAP原理、最终一致性)
    啥叫异步调用
    C++中虚函数的作用是什么?它应该怎么用呢?
    [转]Thrift连接池实现
  • 原文地址:https://www.cnblogs.com/wxc-xiaohuang/p/9427867.html
Copyright © 2011-2022 走看看