【问题标题】:Calling an endpoint inside of a class in SpringBoot [duplicate]在 SpringBoot 中调用类内部的端点 [重复]
【发布时间】:2020-05-22 18:54:39
【问题描述】:

我有一个端点,我需要从类中的方法调用。

public void remove(String str) {
    //Call Controller with str
}

控制器有/local/{str}。如何从该方法调用此控制器?

【问题讨论】:

标签: java spring-boot


【解决方案1】:

所以我想你的控制器类似于这个

@GetMapping("/local/{str}")
public String method(@PathVariable String str) {...}

为什么不直接调用这个方法呢?

public void remove(String str) {
    method("my parameter");
}

或者你也可以使用RestTemplate调用这个端点

public void remove(String str) {
    final String uri = "http://hostName/local/{str}";
    Map<String, String> params = new HashMap<String, String>();
    params.put("str", "my_String");

    RestTemplate restTemplate = new RestTemplate();
    String response = restTemplate.getForObject(uri, String.class, params);
}

【讨论】:

    猜你喜欢
    • 2015-11-22
    • 2020-10-25
    • 2021-05-31
    • 1970-01-01
    • 2011-02-18
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多