API 正在传输另一个 API 响应



我和我的团队正在使用GWT 2.4(JDK 1.6.0_45(开发一些古老但相当大的应用程序。

我们目前正面临一个使用 HTTP 协议的公共 API。他们最近切换到Java 6(免费版本(不太使用的HTTPS。

我有多种解决方案:

  • 升级到可维护但不是免费的 Java 6 版本(我们希望避免付费(
  • 升级到Java 8(GWT
  • 2.4与Java 8不兼容,因此我们还必须升级到GWT 2.8,考虑到应用程序的大小,这将需要一些时间(
  • 开发一个小 API,捕获此公共 API 的响应并使用 HTTP 协议将其发送回我的应用程序

我开始了第三个解决方案,但是在解组收到的响应(xml(时遇到了一些问题。

这是我到目前为止所做的:

调用公共 API 的 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);
}

调用公共 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类。 问题是,您需要指定响应的性质,由于我的解组问题,我想避免这种情况。

我的问题是:是否可以将此公共 API 的响应传输到我的应用程序,而无需在我的 API 接收时使用它?(简单地说,复制/粘贴它(

我最终使用HttpCliendBuilder来构建我的请求并获得InputStream作为响应。 这样,我可以将其转换为字符串并使用它创建一个响应实体。

最新更新