1.项目配置文件

项目主Application编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.javawiki.wiki_02;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;

@SpringBootApplication
public class Wiki02Application {


private static final Logger LOG = LoggerFactory.getLogger(Wiki02Application.class);

public static void main(String[] args) {
SpringApplication app = new SpringApplication(Wiki02Application.class);
Environment env = app.run(args).getEnvironment();
LOG.info("启动成功!!");
LOG.info("地址: \thttp://127.0.0.1:{}", env.getProperty("server.port"));
}

}

main/java/resoures/application.properties编写启动端口

1
server.port=8080

2.接口开发

在项目 main/java/项目名称主目录下新建 一个package,名称为controller

image-20240203230243259

之后新建一个测试类,TestController,编写代码

  • @RestController 返回字符串格式
  • @RequestMapping(“/hello”) // 设置hello接口 @RequestMapping 支持所有的请求方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.javawiki.wiki_02.controller;

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


@RestController // @RestController 返回字符串格式
public class TestController {

/*
* GET,POST,PUT,DELETE
*
* */
@RequestMapping("/hello") // 设置hello接口 @RequestMapping 支持所有的请求方式
public String hello()
{
return "Hello Wrold";
}
}

启动项目,访问

1
http://127.0.0.1:8080/hello

image-20240203230820458

3.IDEA自带client测试接口插件

根目录新建 http文件夹,里面新增test.http

1
2
3
4
5
6
7
8
9
10
11
GET http://localhost:8080/hello
Accept: application/json

###

POST http://localhost:8080/hello/post
Content-Type: application/x-www-form-urlencoded

name = ZhiHuan

###

点击既能测试

image-20240204123014600

4.遇到的坑

Springboot用3.2.2 Mybatis需要用3.0.3