![](/img/trans.png)
[英]NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V
[英]com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V
我正在使用谷歌BETA API:AutoML自然語言,在培訓API之后,我在Java SE項目中進行了測試,獲得了令人滿意的結果,但是,當我將其遷移到Java EE項目時,我遇到了不同的問題。 關於:
-Java -version:1.8.0_201
-Payara版本:5.191
-google-cloud-automl dependecy:0.97.0-beta
當我嘗試運行實體提取服務(PredictionServiceClient類)時,它返回錯誤java.lang.NoSuchMethodError:com.google.common.base.Preconditions.checkArgument(ZLjava / lang / String; CLjava / lang / Object;)V ,根據不同的來源,這個錯誤是由番石榴庫的不同版本之間的沖突引起的。 我認為我的特殊錯誤是因為guava的版本在payara中是19.0.0而google-cloud-automl需要版本> 20。
為了解決這個錯誤,payara建議在這里修改文件glassfish-application.xml,“這樣,EAR的lib /目錄中包含的庫將優先”,但是這對我不起作用。
代碼是在本文檔的幫助下編寫的
與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而不是payara服務器中使用番石榴版本嗎? 在maven mvn dependecy中測試命令行之后; tree -Dverbose在我的項目的路徑中我沒有得到任何依賴項錯誤。 番石榴真的可以成為這個錯誤的原因嗎? 我覺得我已經嘗試了所有的東西,我找不到可能解決我的問題的方法,提前謝謝你。
在他們的BETA版本中使用AutoML Natural Language API時,他們中的許多人可能會遇到此錯誤和其他錯誤,這是正常的,我們必須假設作為BETA版本會出現這種類型的問題。 我的解決方案是從我的JAVA EE應用程序中分離API的消耗,首次在AWS(Amazon Web Service)中實現lambda函數,並通過對此函數的REST調用來使用該服務。 這對我來說非常完美,而且這是一個干凈的解決方案(從我的角度來看)。 問候。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.