google 愿景 api 在 intelliJ 中导入失败

google vision api failed imports in intelliJ

我正在使用 intelliJ IDE 并尝试过 https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/vision/label

我的 pom.xml 没有显示任何错误,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <packaging>war</packaging>
  <version>1.0-SNAPSHpOT</version>
  <groupId>com.example.endpoints</groupId>
  <artifactId>endpoints</artifactId>

  <parent>
    <artifactId>doc-samples</artifactId>
    <groupId>com.google.cloud</groupId>
    <version>1.0.0</version>
    <relativePath>../..</relativePath>
  </parent>

  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>

    <maven.war.plugin>2.6</maven.war.plugin>

    <appengine.maven.plugin>1.0.0</appengine.maven.plugin>
    <jetty.maven.plugin>9.3.8.v20160314</jetty.maven.plugin>

    <failOnMissingWebXml>false</failOnMissingWebXml> <!-- REQUIRED -->
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.google.apis</groupId>
      <artifactId>google-api-services-vision</artifactId>
      <version>v1-rev347-1.22.0</version>
    </dependency>
      <dependency>
          <groupId>com.google.api-client</groupId>
          <artifactId>google-api-client</artifactId>
          <version>1.22.0</version>
          <exclusions>
              <exclusion> <!-- exclude an old version of Guava -->
                  <groupId>com.google.guava</groupId>
                  <artifactId>guava-jdk5</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>20.0</version>
      </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <type>jar</type>
      <scope>provided</scope>
    </dependency>
    <!--  Gson: Java to Json conversion -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.6.2</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <!-- for hot reload of the web application -->
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
    <plugins>
      <plugin>  <!-- TEMPORARY -->
        <groupId>com.google.appengine</groupId>
        <artifactId>gcloud-maven-plugin</artifactId>
        <version>2.0.9.121.v20160815</version>
      </plugin>
      <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>appengine-maven-plugin</artifactId>
        <version>${appengine.maven.plugin}</version>
        <configuration>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>${maven.war.plugin}</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jetty.maven.plugin}</version>
      </plugin>
    </plugins>
  </build>
</project>

但是,java class 无法解决

的依赖关系
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.vision.v1.Vision;
import com.google.api.services.vision.v1.VisionScopes;
import com.google.api.services.vision.v1.model.AnnotateImageRequest;
import com.google.api.services.vision.v1.model.AnnotateImageResponse;
import com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest;
import com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse;
import com.google.api.services.vision.v1.model.EntityAnnotation;
import com.google.api.services.vision.v1.model.Feature;
import com.google.api.services.vision.v1.model.Image;
import com.google.common.collect.ImmutableList;

我试过了 mvn -U 全新安装

但运气不好

因为您使用的是 Flex 环境。我建议如下

(1) 按照 Java 中的 Flex 快速入门进行操作以确保您的设置正确:https://cloud.google.com/appengine/docs/flexible/java/quickstart

  • 首先在本地测试(使用码头,如快速入门所示)
  • 如果您愿意,之后可以部署到 App Engine Flex

(2) 然后,一旦你知道一切正常(至少在本地使用码头服务器),你就可以向你的 pom.xml 添加以下内容

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-vision</artifactId>
    <version>0.9.4-beta</version>
</dependency>

这来自这个页面:https://cloud.google.com/vision/docs/reference/libraries#client-libraries-install-java

您还可以在其中找到代码示例。

(3) 顺便说一句:对于 Auth,我建议在您的机器上本地设置一个服务帐户。为此,您需要在 Cloud Console 中创建一个服务帐户,然后在本地下载一个 .json 文件,然后设置环境变量 GOOGLE_APPLICATION_CREDENTIALS 以指向该 .json 文件.