gpt4 book ai didi

java - com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V

转载 作者:行者123 更新时间:2023-12-02 04:06:16 25 4
gpt4 key购买 nike

我正在使用谷歌BETA API:AutoML Natural Language,在训练该API之后,我在Java SE项目中进行了测试,获得了满意的结果,但是,当我将其迁移到Java EE项目时,我遇到了不同的问题。关于:

-Java-版本:1.8.0_201
-Payara版本:5.191
-google-cloud-automl 依赖项:0.97.0-beta

当我尝试运行实体提取服务(PredictionServiceClient 类)时,它返回错误 java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument (ZLjava/lang/String; CLjava/lang/Object;)V,根据不同来源此错误是由于不同版本的guava库之间的冲突引起的。我认为我的特殊错误是因为 payara 中 guava 的版本是 19.0.0 而 google-cloud-automl 需要版本 > 20。

要解决此错误 payara 建议 here修改文件 glassfish-application.xml,“这样,EAR 的 lib/目录中包含的库将优先”,但这对我不起作用。

该代码是在 this 的帮助下编写的文档

glassfish-application.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-application PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Java EE Application 6.0//EN" "http://glassfish.org/dtds/glassfish-application_6_0-1.dtd">
<glassfish-application>
<classloading-delegate>false</classloading-delegate>
</glassfish-application>

pom[EJB].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>
<parent>
<artifactId>AutoMLTest</artifactId>
<groupId>co.com.group.automl</groupId>
<version>1.0</version>
</parent>

<groupId>co.com.group.automl</groupId>
<artifactId>AutoMLTest-ejb</artifactId>
<version>1.0</version>
<packaging>ejb</packaging>

<name>AutoMLTest-ejb</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-automl</artifactId>
<version>0.97.0-beta</version>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<targetPath>META-INF</targetPath>
<directory>src</directory>
<includes>
<include>jax-ws-catalog.xml</include>
<include>wsdl/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

方法 AutoML Extraction.xml

public void extracNL(String content) throws Exception {
String googleCredentials = "/path/credentials.json"
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(googleCredentials)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));

PredictionServiceSettings settings =
PredictionServiceSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();

//The problem is presented in this line of code
PredictionServiceClient serviceClient =
PredictionServiceClient.create(settings);

ModelName modelName = ModelName.of("projectId", "computeRegion", "modelId");
TextSnippet snippet = TextSnippet.newBuilder().setContent(content).setMimeType("text/plain").build();
ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(snippet).build();

Map<String, String> params = new HashMap<>();
PredictResponse response = serviceClient.predict(modelName, payload, params);
List<PredictGoogleDTO> predictionsTmp = new ArrayList<>();

for (AnnotationPayload annotationPayload : response.getPayloadList()) {
if (annotationPayload.getDisplayName().equals("Person")) {
System.out.println("DisplayName="+annotationPayload.getDisplayName());
System.out.println("Content="+annotationPayload.getTextExtraction().getTextSegment().getContent());
}
}
}

还有其他方法可以做到这一点吗?我的应用程序真的不可能在pom中使用guava版本而不是payara服务器吗?在maven mvn dependentecy 中测试命令行后;在我的项目路径中的 tree -Dverbose 我没有收到任何依赖项错误。那么 Guava 真的是这个错误的原因吗?我觉得我已经尝试了一切,但找不到可能的解决方案来解决我的问题,提前谢谢您。

最佳答案

在 BETA 版本中使用 AutoML Natural Language API 时,可能许多人都会遇到此错误和其他错误,这是正常的,我们必须假设作为 BETA 版本会出现此类问题。我的解决方案是将 API 的使用与 JAVA EE 应用程序隔离,首次在 AWS(Amazon Web Service)中实现 lambda 函数,并通过对此函数的 REST 调用来使用服务。这对我来说非常有效,并且它是一个干净的解决方案(从我的角度来看)。问候。

关于java - com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56711846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com