跨域支持
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig {
/**
* 跨域支持
*
* @return
/
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true); // 允許cookies跨域
config.addAllowedOrigin("");// #允許向該服務(wù)器提交請(qǐng)求的URI律想,表示全部允許
config.addAllowedHeader("");// #允許訪問(wèn)的頭信息,表示全部
config.setMaxAge(18000L);// 預(yù)檢請(qǐng)求的緩存時(shí)間(秒)猎莲,即在這個(gè)時(shí)間段里,對(duì)于相同的跨域請(qǐng)求不會(huì)再預(yù)檢了
config.addAllowedMethod("");// 允許提交請(qǐng)求的方法技即,表示全部允許
source.registerCorsConfiguration("/*", config);
return new CorsFilter(source);
}
}
RestTemplate高并發(fā)下異常與配置
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
/**
RestTemplate高并發(fā)下異常與配置說(shuō)明
1著洼、java.util.ConcurrentModificationException
-
2、java.net.SocketTimeoutException Connection timed out
*/
@Configuration
public class RestTemplateConfig {@Bean
@LoadBalanced
public RestTemplate restTemplate() {
// 長(zhǎng)連接
PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager();
// 總連接數(shù)
pollingConnectionManager.setMaxTotal(1000);
// 同路由的并發(fā)數(shù)
pollingConnectionManager.setDefaultMaxPerRoute(1000);HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setConnectionManager(pollingConnectionManager);
// 重試次數(shù)而叼,默認(rèn)是3次身笤,沒(méi)有開(kāi)啟
// httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true));
HttpClient httpClient = httpClientBuilder.build();
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
httpClient);
// 連接超時(shí) ms
clientHttpRequestFactory.setConnectTimeout(12000);
// 數(shù)據(jù)讀取超時(shí)時(shí)間,即SocketTimeout ms
clientHttpRequestFactory.setReadTimeout(12000);
// 連接不夠用的等待時(shí)間澈歉,不宜過(guò)長(zhǎng)展鸡,必須設(shè)置,比如連接不夠用時(shí)埃难,時(shí)間過(guò)長(zhǎng)將是災(zāi)難性的
clientHttpRequestFactory.setConnectionRequestTimeout(200);
// 緩沖請(qǐng)求數(shù)據(jù),默認(rèn)值是true涤久。通過(guò)POST或者PUT大量發(fā)送數(shù)據(jù)時(shí)涡尘,建議將此屬性更改為false,以免耗盡內(nèi)存响迂。
// clientHttpRequestFactory.setBufferRequestBody(false);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(clientHttpRequestFactory);
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
return restTemplate;
}
}
json數(shù)據(jù)Long為String
/**
- @description: 返回json是轉(zhuǎn)換long為string
- @create: 2019-08-02 17:49
**/
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
@EnableWebMvc
@Configuration
public class WebDataConvertConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
/**
* 序列換成json時(shí),將所有的long變成string
* 因?yàn)閖s中得數(shù)字類型不能包含所有的java long值
*/
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
jackson2HttpMessageConverter.setObjectMapper(objectMapper);
converters.add(jackson2HttpMessageConverter);
}
}
本文由作者pm1280415703:JAVA實(shí)驗(yàn)手冊(cè) 發(fā)布,交流群:583284584考抄!