SpringBoot(1)——HelloWorld程序

1 HelloWorld

1.1 从IEDA中创建

在这里插入图片描述
点击next,选择一下操作
在这里插入图片描述
选择项目的安装路径即可
在这里插入图片描述

1.2 程序编写

1 创建如图所示的目录结构
在这里插入图片描述
2 HelloController.java

package com.zs.helloword.controller;

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

// 本身就是一个spring的组件
@RestController
public class HelloController {
    
    
    // 接口 http://localhost:8080/hello
    @RequestMapping("/hello")
    public String hello() {
    
    
        // 调用业务 接受前端的参数
        return "Hello World";
    }
}

3 运行HellowordApplication.java(注意:所创建的文件夹与HellowordApplication.java要在同一级下)

1.3 文件介绍

1 application.properties:springboot的核心配置文件,如

# springboot 核心配置文件
# 更改端口号
#server.port= 8081

2 pom.xml

<?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.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zs</groupId>
    <artifactId>helloword</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloword</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- web场景启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springboot单元测试 -->
        <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>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4.2</version>
            </plugin>

        </plugins>
    </build>

</project>

猜你喜欢

转载自blog.csdn.net/zs18753479279/article/details/112390137
今日推荐