我正在使用google : AutoML自然语言,在培训该API之后,我在Java项目中进行了测试,获得了令人满意的结果,但是,当我将它移植到Java项目时,我遇到了不同的问题。关于:
-Java -version: 1.8.0_201
-Payara版本: 5.191
-谷歌-云-自动依赖: 0.97.0-beta
当我尝试运行实体提取服务(PredictionServiceClient类)时,它返回错误java.lang.NoSuchMethodError: java.lang.NoSuchMethodError (ZLjava / lang / String;CLjava / lang / Object;) V,根据不同的来源,此错误是由不同版本的番石榴库之间的冲突引起的。我认为我的错误是因为番石榴的版本在payara中是19.0.0,而需要一个版本大于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>pomEJB.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依赖关系;tree -Dverbose中的命令行之后,我不会收到任何依赖项错误。那么番石榴真的是造成这一错误的原因吗?我觉得我已经尝试了一切,我无法找到一个可能的解决办法,我的问题,事先谢谢你。
发布于 2019-07-25 17:29:45
在他们的BETA版本中使用AutoML自然语言API时,他们中的许多人可能会出现这个错误和其他错误,这是正常的,我们必须假设,作为BETA版本,这种类型的问题就会出现。我的解决方案是将API的使用与我的JAVA应用程序隔离开来,在AWS (Amazon )中首次实现lambda函数,并通过REST调用该函数来使用该服务。这对我来说是完美的,这是一个干净的解决方案(从我的角度来看)。致以问候。
https://stackoverflow.com/questions/56711846
复制相似问题