1.Fle->New->Project,选择Maven,点击Next
2.修改项目名称,点击Finish
3.项目创建完毕,等待Maven下载完成
4.修改pom.xml文件,改成如下内容
java
<?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>hello</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.demo.DemoApplication</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
5.创建一个package,com.demo,然后创建一个java程序DemoApplication,代码如下,等待Maven下载完毕
java
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
6.tomcat默认端口号是8080,如果需要修改,可以自行配置端口号,首先在resources目录下创建一个新的file,名称填resource.properties,内容填写deploy.mode=application
7.在resources目录下创建一个新的文件夹config,然后创建一个新的文件application.yml,比如默认端口号改成8808,web项目跟路径改成hello
java
server:
port: 8808
servlet:
context-path: /hello
spring:
profiles:
active: hello
8.创建一个java类HelloController,代码如下
java
package com.demo;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping("/sayHello")
public @ResponseBody
String sayHello( @RequestParam(name = "name") String name) {
return "hello:"+name;
}
}
9.右键点击HelloController,Run,浏览器输入http://localhost:8808/hello/hello/sayHello?name=123即可看到效果