场景:在项目中需要请求第三方接口,但第三方接口暂时无法使用,仅已知接口文档,可以使用一个demo工程模拟外部接口的数据,用于临时测试,达到不影响开发进度的目的

​​

​​

package com.example.demo;

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@GetMapping("/aaa/getData")
	public Map<String, Object> getCgkRealData() {
		// 构建返回的数据
		Map<String, Object> response = new HashMap<>();
		response.put("code", "success");

		// 创建数据列表
		List<Map<String, String>> dataList = List.of(
				Map.of(
						"datetime", "2024-09-04 05:00:00",
						"no", "6102",
						"temp", "12.81"
				)
		);

		response.put("data", dataList);

		return response;
	}
}

在原项目中请求

shuiWenJCCGKBaseEntity = restTemplate.getForObject("http://127.0.0.1:9091" + "/swjc/getCgkRealData", BaseEntity.class);