SpringBoot:实例一

一、实现的效果

在浏览器地址栏输入http://localhost:8080/hello,当前页面显示hello world

实例一代码:点击查看LearnSpringBoot01

点击查看更多的SpringBoot教程

二、 效果图

三、 pom.xml代码

bash 复制代码
<?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 http://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.7.0</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>LearnSpringBoot01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>15</java.version>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!--
        Spring Boot插件,将应用打包成一个可执行的jar包
         原文链接:https://blog.csdn.net/qheuwq/article/details/130953123
    -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

四、HelloController.java代码

bash 复制代码
package org.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

//这个类所有方法返回的数据直接写给浏览器,(如果是对象转换为json数据)
//@ResponseBody
//@Controller
@RestController // 等于上面 ResponseBody 加 Controller
public class HelloController {
//    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "hello world";
    }
}

五、HelloWorldMainApplication.java代码

bash 复制代码
package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;

/**
 * https://www.bilibili.com/video/av59572480/?p=5&spm_id_from=pageDriver&vd_source=4110e082785c06a8c24a5c86c6182472
 *
 * SpringBootApplication 来标注一个主程序类,说明这是一个spring boot应用
 *
 * SpringBoot出现找不到或无法加载主类解决办法  : 点击项目的目录,鼠标右键选择Maven->Reload Project,或者进行mvn clean后重新运行
 * https://blog.csdn.net/github_38924695/article/details/128236420
 */
//@RestController
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        //Spring 来启动应用
        SpringApplication.run(HelloWorldMainApplication.class, args);
    }
}

六、启动服务

相关推荐
一条Go34 分钟前
接收方合并去重:同一个人为什么只该收到一次通知
后端·架构·github
北漂人Java43 分钟前
基于 Spring AI Alibaba WorkFlow 构建 Java 生产级 AI Agent
java·人工智能·spring
张一凡931 小时前
给 Coding Agent 加一道"架构闸门":Jin 是如何做到"先审架构、再写代码、不漂移"的
前端·后端·agent
Jvav练习生1 小时前
AI Agent 总在关键时刻“失忆”翻车?一文讲透上下文工程(附可运行 Python 代码)
后端
用户112936112191 小时前
编译全绿,上线就崩:MyBatis 五层方法名大小写埋下的坑
java
leo在掘金1 小时前
只挂了11小时,5000台开发机被塞进SSH公钥
后端·架构
xjz18422 小时前
Rust 运算符优先级:括号不只用来改变顺序
后端
写码跑山的老艾2 小时前
为什么有了 MySQL,还要 Redis 和 Elasticsearch?从一件商品看数据系统的权衡
后端
对象存储与RustFS2 小时前
Jenkins 的构建产物别堆在控制器磁盘:用 RustFS 当它的 S3 工件仓库
后端·docker·云原生