下面列出了怎么用org.springframework.boot.context.properties.PropertyMapper的API类实例代码及写法,或者点击链接到github查看源代码。
protected HttpClientConfig createHttpClientConfig(BeihuMutilElasticsearchJestProperties.Instances instance) {
HttpClientConfig.Builder builder = new HttpClientConfig.Builder(
instance.getUris());
PropertyMapper map = PropertyMapper.get();
map.from(instance::getUsername).whenHasText().to((username) -> builder
.defaultCredentials(username, instance.getPassword()));
map.from(this.gsonProvider::getIfUnique).whenNonNull().to(builder::gson);
map.from(instance::isMultiThreaded).to(builder::multiThreaded);
map.from(instance::getConnectionTimeout).whenNonNull()
.asInt(Duration::toMillis).to(builder::connTimeout);
map.from(instance::getReadTimeout).whenNonNull().asInt(Duration::toMillis)
.to(builder::readTimeout);
customize(builder);
return builder.build();
}
/**
* 异步httpclient连接数配置
*/
private void setHttpClientConfig(RestClientBuilder builder, RestClientPoolProperties poolProperties, RestClientProperties restProperties){
builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setMaxConnTotal(poolProperties.getMaxConnectNum())
.setMaxConnPerRoute(poolProperties.getMaxConnectPerRoute());
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(restProperties::getUsername).to(username -> {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(username, restProperties.getPassword()));
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
});
return httpClientBuilder;
});
}
public CorsConfiguration toCorsConfiguration() {
if (CollectionUtils.isEmpty(this.allowedOrigins)) {
return null;
}
PropertyMapper map = PropertyMapper.get();
CorsConfiguration configuration = new CorsConfiguration();
map.from(this::getAllowedOrigins).to(configuration::setAllowedOrigins);
map.from(this::getAllowedHeaders).whenNot(CollectionUtils::isEmpty)
.to(configuration::setAllowedHeaders);
map.from(this::getAllowedMethods).whenNot(CollectionUtils::isEmpty)
.to(configuration::setAllowedMethods);
map.from(this::getExposedHeaders).whenNot(CollectionUtils::isEmpty)
.to(configuration::setExposedHeaders);
map.from(this::getMaxAge).whenNonNull().as(Duration::getSeconds)
.to(configuration::setMaxAge);
map.from(this::getAllowCredentials).whenNonNull()
.to(configuration::setAllowCredentials);
return configuration;
}
@Bean
public ReactorNettyRequestUpgradeStrategy reactorNettyRequestUpgradeStrategy(
HttpClientProperties httpClientProperties) {
ReactorNettyRequestUpgradeStrategy requestUpgradeStrategy = new ReactorNettyRequestUpgradeStrategy();
HttpClientProperties.Websocket websocket = httpClientProperties
.getWebsocket();
PropertyMapper map = PropertyMapper.get();
map.from(websocket::getMaxFramePayloadLength).whenNonNull()
.to(requestUpgradeStrategy::setMaxFramePayloadLength);
map.from(websocket::isProxyPing).to(requestUpgradeStrategy::setHandlePing);
return requestUpgradeStrategy;
}
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(this.serverProperties::getPort).to(factory::setPort);
map.from(this.serverProperties::getAddress).to(factory::setAddress);
map.from(this.serverProperties.getServlet()::getContextPath).to(factory::setContextPath);
map.from(this.serverProperties.getServlet()::getApplicationDisplayName).to(factory::setDisplayName);
map.from(this.serverProperties.getServlet()::getSession).to(factory::setSession);
map.from(this.serverProperties::getSsl).to(factory::setSsl);
map.from(this.serverProperties.getServlet()::getJsp).to(factory::setJsp);
map.from(this.serverProperties::getCompression).to(factory::setCompression);
map.from(this.serverProperties::getHttp2).to(factory::setHttp2);
map.from(this.serverProperties::getServerHeader).to(factory::setServerHeader);
map.from(this.serverProperties.getServlet()::getContextParameters).to(factory::setInitParameters);
}
public SslCustomizer(AbstractConfigurableWebServerFactory factory) {
this.factory = factory;
this.propertyMapper = PropertyMapper.get();
}