为java

时间:2017-03-22 16:17:57

标签: java eclipse google-cloud-endpoints

我使用java在eclipse中有一个示例google cloud端点项目。

我是使用https://cloud.google.com/endpoints/docs/frameworks/java/quickstart-frameworks-java

完成的

然后将项目导入eclipse并转换为标准环境的app引擎项目。

到目前为止一切正常。但是,我在java中添加了一个新的API,如下所示:

package photography.viks.ig;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiIssuer;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;

import photography.viks.ig.bean.MyBean;

@Api(
        name = "instaSmartTaggerSvc",
        version = "v1",
        namespace =
          @ApiNamespace(
            ownerDomain = "viks.photography",
            ownerName = "viks.photography",
            packagePath = ""
          ),
        // [START_EXCLUDE]
        issuers = {
          @ApiIssuer(
            name = "firebase",
            issuer = "https://securetoken.google.com/instasmarttagger",
            jwksUri = "https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com")
        }
        // [END_EXCLUDE]
        )
    // [END echo_api_annotation]
public class InstaSmartTaggerSvc {
    @ApiMethod(name = "analyzeImage", httpMethod = ApiMethod.HttpMethod.GET)
      public MyBean analyzeImage() {
        return new MyBean("analysis done");
      }
}

和web.xml看起来像

<web-app xmlns="http://java.sun.com/xml/ns/javaee"  version="2.5">
    <!-- Wrap the backend with Endpoints Frameworks v2. -->
    <servlet>
        <servlet-name>EndpointsServlet</servlet-name>
        <servlet-class>com.google.api.server.spi.EndpointsServlet</servlet-class>
        <init-param>
            <param-name>services</param-name>
            <param-value>com.example.echo.Echo,photography.viks.ig.InstaSmartTaggerSvc</param-value>
        </init-param>
    </servlet>
    <!-- Route API method requests to the backend. -->
    <servlet-mapping>
        <servlet-name>EndpointsServlet</servlet-name>
        <url-pattern>/_ah/api/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <!-- Add a filter that fetches the service config from service management. -->
    <filter>
        <filter-name>endpoints-api-configuration</filter-name>
        <filter-class>com.google.api.control.ServiceManagementConfigFilter</filter-class>
    </filter>

    <!-- Add a filter that performs Endpoints logging and monitoring. -->
    <filter>
        <filter-name>endpoints-api-controller</filter-name>
        <filter-class>com.google.api.control.extensions.appengine.GoogleAppEngineControlFilter</filter-class>
        <init-param>
            <param-name>endpoints.projectId</param-name>
            <param-value>${endpoints.project.id}</param-value>
        </init-param>
        <init-param>
            <param-name>endpoints.serviceName</param-name>
            <param-value>echo-api.endpoints.${endpoints.project.id}.cloud.goog</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>endpoints-api-configuration</filter-name>
        <servlet-name>EndpointsServlet</servlet-name>
    </filter-mapping>

    <filter-mapping>
        <filter-name>endpoints-api-controller</filter-name>
        <servlet-name>EndpointsServlet</servlet-name>
    </filter-mapping>
</web-app>

该应用程序可以很好地部署到应用引擎。但是,在api explorer中,我新添加的服务不可见

1 个答案:

答案 0 :(得分:0)

您需要生成OpenApi文档并将文件部署到Google Cloud Platform。您可以使用com.google.endpoints.endpoints-framework库通过调用openApiDocs来生成文件。

使用Maven ...

$ mvn clean package
$ mvn endpoints-framework:openApiDocs

以上命令将默认包含来自web.xml中定义的EndpointsServlet的“服务”参数中定义的Api。该命令将在项目的“输出”目录下创建openapi-docs / openapi.json文件。

要部署OpenApi文档,请致电...

$ gcloud endpoints services deploy OUTPUT_DIR/openapi-docs/openapi.json

用项目的实际“输出”目录替换OUTPUT_DIR。就我而言,这是“目标”目录。