Java 下面提到的使用org.springframework.web.client.RestTemplate的风格的潜在问题是什么?
这是我应用程序的主要类Java 下面提到的使用org.springframework.web.client.RestTemplate的风格的潜在问题是什么?,java,spring,http-headers,apache-httpclient-4.x,resttemplate,Java,Spring,Http Headers,Apache Httpclient 4.x,Resttemplate,这是我应用程序的主要类 @SpringBootApplication (scanBasePackages = { "com.xyz.*" }) @EnableAsync @EnableAspectJAutoProxy (proxyTargetClass=true) @EnableScheduling public class XyzApplication { public static void main(String[] args) { SpringApplicati
@SpringBootApplication (scanBasePackages = { "com.xyz.*" })
@EnableAsync
@EnableAspectJAutoProxy (proxyTargetClass=true)
@EnableScheduling
public class XyzApplication {
public static void main(String[] args) {
SpringApplication.run(XyzApplication.class, args);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
builder.requestFactory(new HttpComponentsClientHttpRequestFactory());
return builder.build();
}
}
在多个服务和组件中,此RestTemplate正在自动连接
就像在控制器中一样,这就像
@RestController
@RequestMapping({ "my-api" })
public class CommonController {
@Autowired
AppConfig appConfig;
@Autowired
RestTemplate restTemplate;
@RequestMapping("check")
public String pwa() {
ResponseEntity<String> response = restTemplate.getForEntity(appConfig.getApiConfig().get("ApiURL"), String.class);
if (HttpStatus.OK == response.getStatusCode()) {
return response.getBody().toString();
} else {
Logger.error(this.getClass(), "Api is not working");
}
return null;
}
}
@RestController
@请求映射({“我的api”})
公共类公共控制器{
@自动连线
AppConfig-AppConfig;
@自动连线
rest模板rest模板;
@请求映射(“检查”)
公共字符串pwa(){
ResponseEntity response=restemplate.getForEntity(appConfig.getApiConfig().get(“apirl”),String.class);
if(HttpStatus.OK==response.getStatusCode()){
返回response.getBody().toString();
}否则{
Logger.error(this.getClass(),“Api不工作”);
}
返回null;
}
}
在一个不同的服务中,比如
@Service
public class DetailsQuery {
@Autowired
private AppConfig appConfig;
@Autowired
private RestTemplate restTemplate;
@Async
public Future<ConcurrentHashMap<String, Object>> getDetails(JSONObject object) throws InterruptedException, RestClientException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpEntity<String> entity = new HttpEntity<String>(object.toString(), headers);
Map<String, Object> jsonObject = restTemplate.postForObject(new URI(appConfig.getApiConfig().get("searchApi")), entity, Map.class);
ConcurrentHashMap<String, Object> response = new ConcurrentHashMap<String, Object>();
response.putAll(jsonObject);
return new AsyncResult<ConcurrentHashMap<String,Object>>(new ConcurrentHashMap<>(response));
}
}
@服务
公共类详细信息{
@自动连线
私有AppConfig-AppConfig;
@自动连线
私有RestTemplate RestTemplate;
@异步的
公共未来getDetails(JSONObject对象)抛出InterruptedException、RestClientException{
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
restemplate.getMessageConverters().add(新映射Jackson2HttpMessageConverter());
HttpEntity=新的HttpEntity(object.toString(),headers);
Map jsonObject=restTemplate.postForObject(新URI(appConfig.getApiConfig().get(“searchApi”)),实体,Map.class);
ConcurrentHashMap响应=新建ConcurrentHashMap();
响应。putAll(jsonObject);
返回新的AsyncResult(新的ConcurrentHashMap(响应));
}
}
问题是这个实现抛出了
出现意外错误(类型=内部服务器错误,
状态=500)。获取请求时的I/O错误
“”:
这是得到生产间歇,即使卷曲要求相同的作品 您可以注意的是,您正在自动连接RestTemplate singleton对象,但是,每次调用该方法时,它都会向RestTemplate添加相同的消息转换器 请记住,resttemplate在构建后是线程安全的,但在构建后处理消息转换器可能不是线程安全的。查看此线程的示例: 您可以尝试执行以下操作,以便仅为您的服务创建一个resttemplate实例
@Service
public class DetailsQuery {
private final RestTemplate restTemplate;
@Autowired
public DetailsQuery (RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.additionalMessageConverters(new MappingJackson2HttpMessageConverter()) build();
}
....
}
或者在创建singleton Resttemplate对象的
@Config
类中执行相同的操作。您可以看到的是,您正在自动连接Resttemplate singleton对象,但是,每次调用该方法时,它都会向Resttemplate添加相同的消息转换器
请记住,resttemplate在构建后是线程安全的,但在构建后处理消息转换器可能不是线程安全的。查看此线程的示例:
您可以尝试执行以下操作,以便仅为您的服务创建一个resttemplate实例
@Service
public class DetailsQuery {
private final RestTemplate restTemplate;
@Autowired
public DetailsQuery (RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.additionalMessageConverters(new MappingJackson2HttpMessageConverter()) build();
}
....
}
或者在创建singleton Resttemplate对象的@Config
类中执行相同的操作