首页 > 解决方案 > 从 gwt 调用 Spring MicroService

问题描述

我有一个需要调用 Spring Boot 微服务的 GWT 客户端。我认为它可以类似于调用一个休息网络服务,但有没有更好的方法来做到这一点?

标签: springgwtmicroservices

解决方案


您可能可以使用RequestBuilder从 GWT 应用程序的客户端调用您的 API:

import com.google.gwt.http.client.RequestBuilder;

// ....

try {
    new RequestBuilder(
            RequestBuilder.GET, // GET, POST, etc.
            url                 // url of your microservice endpoint
    ).sendRequest(null, new RequestCallback() { // replace null with your req body if needed
        @Override
        public void onResponseReceived(Request req, Response resp) {
            // Parse resp.getText() which is hopefully a JSON string
        }
        @Override
        public void onError(Request res, Throwable throwable) {
            // handle errors
        }
    });
} catch (RequestException e) {
    // log, rethrow... the usual
}

推荐阅读