如何在运行时更改端点

huangapple go评论70阅读模式
英文:

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(&quot;/api/testing&quot;)
public class TestingController {
    @Autowired
    RequestMappingHandlerMapping mappingHandler;

    @GetMapping(&quot;/switch&quot;)
    public void switchHandler() throws Exception {
        RequestMappingInfo oldInfo = RequestMappingInfo.paths(&quot;/api/testing/old&quot;)
                .methods(RequestMethod.GET)
                .build();
        mappingHandler.unregisterMapping(oldInfo);

        RequestMappingInfo newInfo = RequestMappingInfo.paths(&quot;/api/testing/new&quot;)
                .methods(RequestMethod.GET)
                .build();
        Method method = this.getClass().getDeclaredMethod(&quot;oldHandler&quot;);
        mappingHandler.registerMapping(newInfo, this, method);
    }

    @GetMapping(&quot;/old&quot;)
    public void oldHandler() {
        System.out.println(&quot;old endpoint&quot;);
    }
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月19日 17:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定