已发布类似问题here和here但前者仅向NoSuchMethodError
提出解决方案,而后者甚至不提及ResteasyClientBuilder
,这恰恰是get
每当我尝试从客户端调用standalone
方法时,会导致我的错误。
我使用的是WildFly,Maven,RESTEasy / JBoss。我可以在命令提示符下使用war
成功运行我的WildFly服务器,并使用maven将get
文件部署到WildFly服务器,并在浏览器/邮递员中使用get
方法来接收结果。但是,如果我尝试从我的客户端代码中调用完全相同的ResteasyClientBuilder
方法,我会收到以下错误。它由resteasy-jaxrs-3.0.19.Final.jar
引起。
有什么问题?我的pom中有最小的依赖项和插件,服务器代码可以工作,但客户端没有。我的pom使用版本" 3.0.19.Final"因为那是C:\Users\ME\Documents\Wildfly\wildfly-10.1.0.Final\modules\system\layers\base\org\jboss\resteasy\resteasy-jaxrs\main
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/resteasy/client/jaxrs/ResteasyClientBuilder
at com.sample.ClientDemo.main(ClientDemo.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
的jar版本
错误:
<?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>
<groupId>com.sample</groupId>
<artifactId>rest-demo</artifactId>
<version>1.0.SNAPSHOT</version>
<packaging>war</packaging>
<name>rest-demo</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.19.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.19.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<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-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.1.0.Alpha11</version>
<configuration>
<name>rest-demo.war</name>
</configuration>
</plugin>
</plugins>
</build>
</project>
的pom.xml
import javax.ws.rs.*;
@Path("tutorial")
public class HelloWorld
{
@GET
@Path("helloname/{name}")
public String hello(@PathParam("name") final String name) {
return "Hello " +name;
}
}
服务器代码(HelloWorld.java)
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import javax.ws.rs.core.Response;
public class ClientDemo {
public static void main(String[] args) {
ResteasyClient client = new ResteasyClientBuilder().build(); // <-- Error occurs here
ResteasyWebTarget target = client.target("http://localhost:8080/rest-demo/rest/tutorial/helloname/john");
Response response = target.request().get();
String value = response.readEntity(String.class);
response.close();
}
}
客户端代码(ClientDemo.java)
let spaces = String(repeating:"\u{00A0}",count : columnSpaceCount)
if globalSubviewHeaderStructArray[i].type == "numeric"{
if globalSubviewHeaderStructArray[i].digitsAfterComma != "0" {
var dataNumber = applyComma(comma : globalSubviewHeaderStructArray[i].digitsAfterComma , data : globalSubviewDataArray[indexPath.row][i])
var newString = "\(spaces)\(checkCurrency(option: globalSubviewHeaderStructArray[i].currency, data: dataNumber))\(spaces)"
newLabel.text = newString
}else{
var newString = "\(spaces)\(checkCurrency(option: globalSubviewHeaderStructArray[i].currency, data: globalSubviewDataArray[indexPath.row][i]))\(spaces)"
newLabel.text = newString
}
}else{
var newString = "\(spaces)\(checkCurrency(option: globalSubviewHeaderStructArray[i].currency, data: globalSubviewDataArray[indexPath.row][i]))\(spaces)"
newLabel.lineBreakMode = .byTruncatingMiddle
newLabel.text = newString
}
在说明客户端jar的说明之后,错误仍然存在。有什么问题?
答案 0 :(得分:1)
您需要将maven项目分为两部分。一个用于客户端演示。另一个战争。在战争中,pom只在org.jboss.resteasy上添加依赖:jaxrs-api并提供它。在客户端pom中添加对org.jboss.resteasy的依赖:resteasy-client没有提供(maven exec插件不包括在类路径中提供的依赖) 我把我的reasteasy演示项目放在github上(server,client)。 在你的情况下,它测试额外的功能并需要更多依赖。客户端可以通过mvn exec:exec
工作答案 1 :(得分:0)
你可以解释一下如何从eclipse或cmd运行客户端代码。 如果它来自eclipse,只需将maven依赖项添加到库并运行java程序,或者从cmd开始,然后将maven依赖项添加到类路径。
答案 2 :(得分:0)
Wildfly还可以。我认为这是你的客户端的类路径没有使用它需要的所有jar。这是我用来运行ClientDemo
类的命令:
java -cp "/.../.m2/repository/org/jboss/logging/jboss-logging/3.1.4.GA/jboss-logging-3.1.4.GA.jar:/.../.m2/repository/org/jboss/resteasy/resteasy-client/3.0.19.Final/resteasy-client-3.0.19.Final.jar:/.../.m2/repository/org/jboss/resteasy/resteasy-jaxrs/3.0.19.Final/resteasy-jaxrs-3.0.19.Final.jar:/.../.m2/repository/org/jboss/spec/javax/ws/rs/jboss-jaxrs-api_2.0_spec/1.0.0.Final/jboss-jaxrs-api_3.0_spec-2.0.0.Final.jar:/.../.m2/repository/org/jboss/spec/javax/annotation/jboss-annotations-api_1.2_spec/1.0.0.Final/jboss-annotations-api_1.2_spec-1.0.0.Final.jar:/.../.m2/repository/javax/activation/activation/1.1.1/activation-1.1.1.jar:/.../.m2/repository/org/apache/httpcomponents/httpclient/4.3.6/httpclient-4.3.6.jar:/.../.m2/repository/org/apache/httpcomponents/httpcore/4.3.3/httpcore-4.3.3.jar:/.../.m2/repository/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar:/.../.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar:/.../.m2/repository/commons-io/commons-io/2.1/commons-io-2.1.jar:/.../.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/.../Desktop/javax.ws.rs-api-2.0.jar:." edu.nova.client.ClientDemo
请注意,要运行它,我必须下载javax.ws.rs-api-2.0.jar
,但在我的情况下,Eclipse在IDE运行ClientDemo
时提供它。我不知道IntellJ,但检查启动配置类路径中的内容。