Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在spring boot java应用程序中调用自定义Rest模板_Java_Spring_Spring Boot_Spring Retry_Retrypolicy - Fatal编程技术网

在spring boot java应用程序中调用自定义Rest模板

在spring boot java应用程序中调用自定义Rest模板,java,spring,spring-boot,spring-retry,retrypolicy,Java,Spring,Spring Boot,Spring Retry,Retrypolicy,我有一个运行在2.1.7版上的spring启动应用程序。我正在尝试使用rest模板生成器实现自定义rest模板,以便设置连接和读取超时。自从我在2.1.7上运行以来,我了解到我需要使用Rest模板生成器。我的自定义rest模板的代码如下所示。我需要在代码的其他区域调用此rest模板的帮助,因为我的应用程序的各个组件都将使用此rest模板,但我需要帮助。如果您对此有任何建议,我们将不胜感激。谢谢 public abstract class CustomRestTemplate implements

我有一个运行在2.1.7版上的spring启动应用程序。我正在尝试使用rest模板生成器实现自定义rest模板,以便设置连接和读取超时。自从我在2.1.7上运行以来,我了解到我需要使用Rest模板生成器。我的自定义rest模板的代码如下所示。我需要在代码的其他区域调用此rest模板的帮助,因为我的应用程序的各个组件都将使用此rest模板,但我需要帮助。如果您对此有任何建议,我们将不胜感激。谢谢

public abstract class CustomRestTemplate implements RestTemplateCustomizer {

    public void customize(RestTemplate restTemplate, Integer connectTimeout, Integer readTimeout) {
        restTemplate.setRequestFactory(new SimpleClientHttpRequestFactory());
        SimpleClientHttpRequestFactory template = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
        template.setConnectTimeout(connectTimeout);
        template.setReadTimeout(readTimeout);
    }
}

您不需要扩展定制程序,这是一种过分的做法。最简单、最干净的方法是创建
restemplate
的bean,并将其作为依赖项注入

例如,您可以拥有一个配置并在其中声明bean:

@Configuration
public class WebConfig {

    private int fooConnectTimeout = 4000;
    private int fooReadTimeout = 4000;

    @Bean
    public RestTemplate restTemplate(final RestTemplateBuilder builder) {
        return builder.setConnectTimeout(fooConnectTimeout)
                .setReadTimeout(fooReadTimeout)
                .build();
    }
}
现在只需在类中注入bean,如下所示:

@Service
public class FooService {

    private RestTemplate restTemplate;

    public FooService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    // custom code here....
}

希望这对您有所帮助,您不需要扩展自定义程序,这太过分了。最简单、最干净的方法是创建
restemplate
的bean,并将其作为依赖项注入

例如,您可以拥有一个配置并在其中声明bean:

@Configuration
public class WebConfig {

    private int fooConnectTimeout = 4000;
    private int fooReadTimeout = 4000;

    @Bean
    public RestTemplate restTemplate(final RestTemplateBuilder builder) {
        return builder.setConnectTimeout(fooConnectTimeout)
                .setReadTimeout(fooReadTimeout)
                .build();
    }
}
现在只需在类中注入bean,如下所示:

@Service
public class FooService {

    private RestTemplate restTemplate;

    public FooService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    // custom code here....
}

希望对您有所帮助

您是否尝试过使用@Autowired将RestTemplate注入其他类?您是否尝试过使用@Autowired将RestTemplate注入其他类?