在我的原型项目中,我致力于实现非常简单的REST服务 - 服务器(后端)和实际上也是服务器的客户端(前端)。后端服务器工作正常,但与客户端我遇到了非常非常奇怪的问题。在Eclipse中我有项目 “前端” 有这个bean称为FrontEndPageBean.java:Java尽管在pom中包含了所有内容,但找不到ResteasyClient
package org.mader.demo.frontend;
import javax.faces.bean.*;
import javax.ws.rs.core.*;
/** Bean to show off frontend handling of REST. */
@ManagedBean
public class FrontEndPageBean
{
/**
* Resolve data from backend through REST service.
* @return Data as text.
*/
public String getRestData()
{ // Retrieve RESTful service using client API from JAX-RS 2.0
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://127.0.0.1:8080/backend/rest/data/1");
Response response = target.request().get();
// Read output in string format
String value = response.readEntity(String.class);
System.out.println(value);
response.close();
return value;
}
}
这就是所谓的home.xhtml这样的:
Text resolved by REST from backend: #{frontEndPageBean.restData}
这是我的POM:
<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>org.mader.demo</groupId>
<artifactId>frontend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Frontend App</name>
<description>Demonstration Frontend App.</description>
<properties>
<webapp.directory>src/main/webapp</webapp.directory>
<jboss.home>${env.JBOSS_HOME}</jboss.home>
<!-- plugin versions -->
<version.war.plugin>2.1.1</version.war.plugin>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<!-- other -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>JBOSS_NEXUS</id>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency> <!-- JBoss distributes a complete set of Java EE 7 APIs including a Bill of Materials (BOM). -->
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-7.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Standard libraries. -->
<dependency> <!-- CDI API -->
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency> <!-- Common Annotations API (JSR-250) -->
<groupId>org.jboss.spec.javax.annotation</groupId>
<artifactId>jboss-annotations-api_1.2_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency> <!-- RESTeasy -->
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency> <!-- JSF -->
<groupId>org.jboss.spec.javax.faces</groupId>
<artifactId>jboss-jsf-api_2.2_spec</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin> <!-- To use, run: mvn package wildfly:deploy -->
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.0.0.Beta1</version>
</plugin>
</plugins>
</build>
</project>
问题是Eclipse无法识别ResteasyClientBuilder,ResteasyClient也不是ResteasyWebTarget。在工具提示中,它显示“ResteasyClient无法解析为某种类型”。没有可见的导入选项。它不会编译。它不工作(TM)。根据关于使用REST的各种页面,ResteasyClient client = new ResteasyClientBuilder()。build();是犹太人的方式。只有我能想到的是,有一些东西可以添加到POM中,一些缺失的依赖。访问http://maven-repository.com/artifact/org.jboss.spec/jboss-javaee-all-7.0/1.0.0.Final没有什么帮助,也没有检查ResteasyClient的代码 - 它看起来应该已经被覆盖了。当然我错过了一些东西......
环境:1.7的Java,Eclipse的开普勒,Wildfly 8.0
除了JAX-RS api jar外,你还没有包含任何东西,你需要RestEasy jar和缺少的类。 search.maven.org会帮助你。 –
嗯,我认为org.jboss.resteasy:jaxrs-api做到了。看起来这是不够的。 –
,应该说RestEasy **罐子**,因为你缺少多个。 –