【Springboot开发】hello world

    技术2024-12-18  11

    我使用的是官网下载的idea社区版,没啥额外的功能需求,所以免费的社区版正合适,不折腾。

    maven和springboot插件的配置随便找一找就有了,maven安装完之后,要对配置文件进行修改(设置jdk,jar仓库),镜像下载地址换成国内的服务器。然后进入idea配置maven,完事。

    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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>untitled</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!--SpringBoot版本--> <version>2.3.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.webflow</groupId> <artifactId>spring-webflow</artifactId> <version>2.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.3.1.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> </project>

    新建一个项目,结构如下图。控制器要在主函数的下一级或同一级包

    代码部分

    main方法

    package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MainApplication{ public static void main(String[] args){ SpringApplication.run(MainApplication.class,args); } }

         控制器

    package com.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping("/") public String hello(){ return "hello world cyj"; } }

    main方法启动springboot,启动内置的tomcat,Mapping("/")映射到跟目录?并执行返回语句。

    执行结果

    需要了解控制器@RestController和requestMapping如何处理请求。

    servlet。

    Processed: 0.016, SQL: 9