英文:
How to change endpoint at runtime
问题
如何在运行时更改端点
示例:GET localhost:8080/old-endpoint
如何将相同的端点URL设置为GET localhost:8080/new-point
我发现"${dynamic.endpoint}"并从application.properties中读取,但我不想要它。我试图根据if else块来编辑端点。
我知道路径变量、请求参数或从application.properties中控制,但对我来说都不起作用。
英文:
How to change endpoint at runtime
example: GET localhost:8080/old-endpoint
how to set same endpoint url to GET localhost:8080/new-point
i found that "${dynamic.endpoint}" and read from application.properties but i don't want it. I'm trying to edit endpoint according to if else blocks.
I know that path variable or request param or control from application.properties but it doesn't work for me
答案1
得分: 0
您有 /old
和 /switch
端点。
在调用 /switch
后,/old
将停止工作(因为使用了 unregisterMapping
方法),而 /new
将开始工作(因为使用了 registerMapping
方法)。
新方法处理程序的逻辑作为 Method
从 Java 反射中传递。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.lang.reflect.Method;
@RestController
@RequestMapping("/api/testing")
public class TestingController {
@Autowired
RequestMappingHandlerMapping mappingHandler;
@GetMapping("/switch")
public void switchHandler() throws Exception {
RequestMappingInfo oldInfo = RequestMappingInfo.paths("/api/testing/old")
.methods(RequestMethod.GET)
.build();
mappingHandler.unregisterMapping(oldInfo);
RequestMappingInfo newInfo = RequestMappingInfo.paths("/api/testing/new")
.methods(RequestMethod.GET)
.build();
Method method = this.getClass().getDeclaredMethod("oldHandler");
mappingHandler.registerMapping(newInfo, this, method);
}
@GetMapping("/old")
public void oldHandler() {
System.out.println("old endpoint");
}
}
此代码段包含了以上描述的功能。
英文:
You have /old
and /switch
endpoints.
After calling /switch
, /old
stops working (because of unregisterMapping
) and /new
works instead (because of registerMapping
).
The logic of new method handler is passed as Method
from java reflection.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-java -->
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.lang.reflect.Method;
@RestController
@RequestMapping("/api/testing")
public class TestingController {
@Autowired
RequestMappingHandlerMapping mappingHandler;
@GetMapping("/switch")
public void switchHandler() throws Exception {
RequestMappingInfo oldInfo = RequestMappingInfo.paths("/api/testing/old")
.methods(RequestMethod.GET)
.build();
mappingHandler.unregisterMapping(oldInfo);
RequestMappingInfo newInfo = RequestMappingInfo.paths("/api/testing/new")
.methods(RequestMethod.GET)
.build();
Method method = this.getClass().getDeclaredMethod("oldHandler");
mappingHandler.registerMapping(newInfo, this, method);
}
@GetMapping("/old")
public void oldHandler() {
System.out.println("old endpoint");
}
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论