gpt4 book ai didi

Creating a Mock API Platform Using Spring Boot: How to create an API at runtime?(使用Spring Boot创建模拟API平台:如何在运行时创建API?)

转载 作者:bug小助手 更新时间:2023-10-28 10:09:40 27 4
gpt4 key购买 nike



Currently, I'm trying to develop a Mock API platform in Spring Boot that can create a mock RESTful endpoints for developer testing purposes. Specifically, I need to create a system where user can define their custom API endpoints, request and expected responses similar to swagger, mockapi.io, ...

目前,我正在尝试在Spring Boot中开发一个Mock API平台,该平台可以创建一个用于开发人员测试目的的模拟RESTful端点。具体地说,我需要创建一个系统,在其中用户可以定义他们的自定义API端点、请求和预期的响应,类似于swagger、mock api.io、...


For starter, I try a naive solution which compile a controller Java class at runtime and try to access that API endpoint but not working. Seem like, the Spring Bean Container cannot understand a new Controller Bean at runtime.

对于初学者,我尝试了一种简单的解决方案,即在运行时编译一个控制器Java类,并尝试访问该API端点,但无法正常工作。看起来,Spring Bean容器在运行时无法理解新的Controller Bean。


App.java

App.java


    public static void main(String[] args) throws ClassNotFoundException, IOException {
var context = SpringApplication.run(ApiSimulationApplication.class, args);
CreationService service = new CreationService();
Path mockControllerPath = service.createMockControllerFile();
service.compileFile(mockControllerPath);
}

MockController.java (created at runtime)

Java(在运行时创建)


@RestController
public class MockController {
public MockController() {}

@GetMapping({"/mock"})
public String sayHello() {return "Hello";}
}


Even if I try to refresh the ApplicationContex, it will throw:

即使我尝试刷新ApplicationContex,它也会抛出:



GenericApplicationContext does not support multiple refresh attempts



I could not think of any other solution or may be I have been following the wrong direction, so I am here to seek for advice and recommendations.
I am looking forward and really appreciate your help.

我想不出任何其他的解决办法,或者可能是我走错了方向,所以我在这里征求意见和建议。我很期待,真的很感谢你的帮助。


更多回答
优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com