程序目录:
编写pom文件:
<?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>com.wyt</groupId>
<artifactId>springhelloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- <version>2.4.2</version>-->
</plugin>
</plugins>
</build>
<!-- <pluginRepositories>-->
<!-- <pluginRepository>-->
<!-- <id>alimaven spring plugin</id>-->
<!-- <name>alimaven spring plugin</name>-->
<!-- <url>https://maven.aliyun.com/repository/spring-plugin</url>-->
<!-- </pluginRepository>-->
<!-- </pluginRepositories>-->
</project>
我用的是2.4.2 切记要按照自己的版本编写。
在application.yml中设置端口号为8070
编写控制类
package com.wyt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class springbootHelloApplication {
public static void main(String[] args) {
SpringApplication.run(springbootHelloApplication.class, args);
}
}
控制器
package com.wyt.contorller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController//@Contorller+@ResponseBody 直接返回字符串
public class HelloWorldControl {
@RequestMapping("/helloworld")
public String show(){
return "hello world";
}
}