SpringBoot基础01:创建项目和编写HTTP接口

创建项目

  • 创建新项目
    选择File->New Module->Spring Initializr,配置相关参数

springboot接口指定form提交 springboot编写对外http接口_xml

)

通过上面步骤完成基础项目的创建,会自动生成以下文件。

  • 程序的主程序类
  • 一个application.properties 配置文件
  • 一个测试类

生成的”DemoApplication“(项目类)和“DemoApplicationTests”(测试类)都可以直接运行来启动当前创建的项目,由于目前该项目为配合任何数据访问或Web模块,程序会加载完Spring之后结束运行。

springboot接口指定form提交 springboot编写对外http接口_maven_02

此时可通过网页:http://localhost:8080/ 来访问此时所运行的项目,判断项目是否搭建成功

  • pom.xml分析
    打开pom.xml,看看Spring Boot项目的依赖:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lurenj</groupId>
    <artifactId>springboot-01-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-01-helloworld</name>
    <description>Lurenj first springboot project</description>
    <properties>
        <java.version>1.8</java.version>
    </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

如上所示,主要有四个部分:

  1. 项目元数据信息:创建时候输入的Project Metadata部分,也就是Maven项目的基本元素,包括:groupId、artifactId、version、name、description等
  2. parent:继承spring-boot-starter-parent的依赖管理,控制版本与打包等内容
  3. dependencies:项目具体依赖,这里包含了spring-boot-starter-web用于实现HTTP接口(该依赖中包含了Spring MVC),官网对它的描述是:使用Spring MVC构建Web(包裹RESTful)应用程序的入门者,使用Tomcat作为默认嵌入式容器。spring-boot-starter-test用于编写单元测试的依赖包。更多功能模块的使用我们将在后面逐步展开。
  4. build:构建配置部分。默认使用了spring-boot-maven-plugin,配合spring-boot-starter-parent就可以把Spring Boot应用打包成JAR来直接运行

编写HTTP接口

  1. 在主入口程序同级目录下创建包体 ,新建一个controller包【一定要在同一级目录下,否则识别不到】

springboot接口指定form提交 springboot编写对外http接口_spring_03


  1. 在包中新建一个Controller类
package com.lurenj.controller;

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

//自动装配
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        //调用业务,接受前端的参数
        return "hello world!";
    }
}
  1. 编写完毕后,从主程序启动项目,浏览器发起请求,查看页面返回
  • 控制台输出了SpringBoot的banner
  • 控制条输出来Tomcat访问的端口号
  • 访问hello请求,字符串成功返回!http://localhost:8080/hello

springboot接口指定form提交 springboot编写对外http接口_xml_04

springboot接口指定form提交 springboot编写对外http接口_maven_05

  1. 将项目打成jar包
  2. 打成了jar包后,就可以在任何地方运行了
  3. 访问地址更改
    可以通过@RequestMapping()和@GetMapping()注释来更改请求地址
package com.lurenj.controller;

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

//自动装配:http://localhost:8080/user/hello
@RestController// 相当于 @Controller+@ResponseBody
@RequestMapping("/user")//一层
public class HelloController {
    @GetMapping("/hello")//二层
    public String hello(){
        //调用业务,接受前端的参数
        return "hello world!";
    }
}
  • @RequestMapping可以指定GET、POST请求方式
  • @GetMapping等价于@RequestMapping的GET请求方式
  1. 更改访问项目端口号

通过application.properties(核心配置文件)调整server.port变量参数更改访问端口号

springboot接口指定form提交 springboot编写对外http接口_spring_06

  1. 更改Spring boot banner
    在resources文件夹下新建banner.txt将寻找到的资源粘进txt文件中,banner就自动修改了

springboot接口指定form提交 springboot编写对外http接口_maven_07