在Spring的Spring 5.0中的SpringTemplate上设置基本URL / URI

时间:2018-03-29 22:12:07

标签: spring spring-boot

我在Spring Boot 2应用中使用Spring 5的RestTemplate,并尝试在其上设置基本URL / URI,这样我就不必在前置每个请求。这也允许我根据属性设置此值。

以前版本的ReleaseTemplate允许您通过构造函数设置基本网址(例如new ReleaseTemplate(baseUrl))。但现在不再是这种情况了。

我看到DefaultUriTemplateHandler类有一个setBaseUrl(),它继承自AbstractUriTemplateHandler接口,但该类已被弃用。建议的替换类DefaultUriBuilderFactory没有任何此类方法。

任何建议将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

尝试使用以下内容: -

String BASE_URI_TEMPLATE = "http://localhost:8080";

DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(BASE_URI_TEMPLATE);

RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(uriBuilderFactory);
restTemplate.getForObject("/test", String.class); // like you may have used earlier
相关问题