Api 正在传输另一个 Api 回复
Api transfering another Api response
我和我的团队正在使用 GWT 2.4 (JDK 1.6.0_45) 开发一些旧的但相当大的应用程序。
我们目前面临 public API 使用 HTTP 协议。他们最近切换到 HTTPS,Java6(免费版)没有很好地使用它。
我有多种解决方案:
- 升级到可维护的,但不是免费的,Java 6 版本(我们希望避免付费)
- 升级到Java 8(GWT 2.4与Java 8不兼容,所以我们也必须升级到GWT 2.8,考虑到应用程序的大小,这需要一些时间)
- 开发一个小型 Api 捕获此 public API 的响应并使用 HTTP 协议将其发送回我的应用程序
我开始了第三个解决方案,但在解组收到的响应 (xml) 时遇到了一些问题。
这是我到目前为止所做的:
我的 API 方法调用 public API:
@Override
public ResponseEntity<WorkMetadataType> lookupWithFilter(String authorization, String filter, String id, Optional<String> accept, Optional<String> xISANAuthorization, Optional<String> idtype) {
WorkMetadataType res = isanApi.lookupWithFilter(authorization, filter, id, accept.orElse(null), xISANAuthorization.orElse(null), idtype.orElse(null));
if (res == null) {
throw new WorkNotFoundException();
}
return ResponseEntity.ok(res);
}
方法调用 public API.
public WorkMetadataType lookupWithFilter(String authorization, String filter, String id, String accept, String xISANAuthorization, String idtype) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(getHttpClient()));
try {
CustomMarshallingHttpMessageConverter converter;
converter = new CustomMarshallingHttpMessageConverter(JAXBContext.newInstance(ISANDataType.class));
converter.setDefaultCharset(StandardCharsets.UTF_8);
restTemplate.getMessageConverters().add(converter);
} catch (JAXBException e) {
logger.error("Erreur lors de la définition du marshaller", e);
}
HttpEntity<String> entity = new HttpEntity<>(null, getHeaders(authorization, accept, xISANAuthorization));
return restTemplate.exchange(getRequestUri(id, idtype, filter), HttpMethod.GET, entity, WorkMetadataType.class).getBody();
}
如您所见,我正在使用 Spring 和他的 RestTemplate class。问题是,您需要指定响应的性质,由于我的解组问题,我想避免这种性质。
我的问题是:是否可以将此 public API 的响应传输到我的应用程序,而无需在我的 API 收到时使用它? (简单地说,做一个 copy/paste)
我终于使用 HttpClientBuilder 构建我的请求并获得 InputStream 作为响应。
这样我就可以将它转换成字符串并用它创建一个 ResponseEntity。
我和我的团队正在使用 GWT 2.4 (JDK 1.6.0_45) 开发一些旧的但相当大的应用程序。
我们目前面临 public API 使用 HTTP 协议。他们最近切换到 HTTPS,Java6(免费版)没有很好地使用它。
我有多种解决方案:
- 升级到可维护的,但不是免费的,Java 6 版本(我们希望避免付费)
- 升级到Java 8(GWT 2.4与Java 8不兼容,所以我们也必须升级到GWT 2.8,考虑到应用程序的大小,这需要一些时间)
- 开发一个小型 Api 捕获此 public API 的响应并使用 HTTP 协议将其发送回我的应用程序
我开始了第三个解决方案,但在解组收到的响应 (xml) 时遇到了一些问题。
这是我到目前为止所做的:
我的 API 方法调用 public API:
@Override
public ResponseEntity<WorkMetadataType> lookupWithFilter(String authorization, String filter, String id, Optional<String> accept, Optional<String> xISANAuthorization, Optional<String> idtype) {
WorkMetadataType res = isanApi.lookupWithFilter(authorization, filter, id, accept.orElse(null), xISANAuthorization.orElse(null), idtype.orElse(null));
if (res == null) {
throw new WorkNotFoundException();
}
return ResponseEntity.ok(res);
}
方法调用 public API.
public WorkMetadataType lookupWithFilter(String authorization, String filter, String id, String accept, String xISANAuthorization, String idtype) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(getHttpClient()));
try {
CustomMarshallingHttpMessageConverter converter;
converter = new CustomMarshallingHttpMessageConverter(JAXBContext.newInstance(ISANDataType.class));
converter.setDefaultCharset(StandardCharsets.UTF_8);
restTemplate.getMessageConverters().add(converter);
} catch (JAXBException e) {
logger.error("Erreur lors de la définition du marshaller", e);
}
HttpEntity<String> entity = new HttpEntity<>(null, getHeaders(authorization, accept, xISANAuthorization));
return restTemplate.exchange(getRequestUri(id, idtype, filter), HttpMethod.GET, entity, WorkMetadataType.class).getBody();
}
如您所见,我正在使用 Spring 和他的 RestTemplate class。问题是,您需要指定响应的性质,由于我的解组问题,我想避免这种性质。
我的问题是:是否可以将此 public API 的响应传输到我的应用程序,而无需在我的 API 收到时使用它? (简单地说,做一个 copy/paste)
我终于使用 HttpClientBuilder 构建我的请求并获得 InputStream 作为响应。 这样我就可以将它转换成字符串并用它创建一个 ResponseEntity。