【发布时间】:2020-02-13 14:11:51
【问题描述】:
我和我的团队正在使用 GWT 2.4 (JDK 1.6.0_45) 开发一些旧但相当大的应用程序。
我们目前正面临一个使用 HTTP 协议的public API。他们最近切换到 HTTPS,Java 6(免费版)没有很好地使用它。
我有多种解决方案:
- 升级到可维护但不是免费的 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 接收时消耗它? (简单地说,复制/粘贴)
【问题讨论】:
-
与您的问题没有直接关系,但如果您将授权或其他敏感数据传递给 api,您可能真的想升级到 https。
-
不幸的是,哪个 Java 6(免费版)正在苦苦挣扎......
-
如果你使用
Resource作为请求的目标类,这有一个getInputStream()方法(如果是isReadable()),你可以使用它直接传输接收到的数据(假设您在RestTemplate上注册了ResourceHttpMessageConverter)。 -
在你的应用程序(ngix,apache httpd)前面放置一些东西,它执行 SSL/HTTPS 的东西并在内部使用 http。
-
@M. Deinum,确实可以更快。我从来没有和他们合作过,所以我会朝这个方向深入一点,谢谢。
标签: java xml spring api unmarshalling