gpt4 book ai didi

java - Dropwizard 客户端类型不匹配

转载 作者:行者123 更新时间:2023-12-01 09:02:46 26 4
gpt4 key购买 nike

首先介绍一些背景知识,我正在尝试在 Eclipse 中设计一个 RESTfull 应用程序,并且我一直在使用此处提供的 pdf 书籍自学 DropWizard:https://github.com/adhulappanavar/Dropwizard/blob/master/RESTful%20Web%20Services%20with%20Dropwizard%20%5BeBook%5D.pdf

这本书大约有两年了,因此我遇到了一些错误,但是我的应用程序入口类(rappApplication)中的以下错误确实困扰着我。

     Exception in thread "main" java.lang.ClassCastException: org.glassfish.jersey.client.JerseyClient cannot be cast to com.sun.jersey.api.client.Client
at edu.udel.rapp.rapp.rappApplication.run(rappApplication.java:50)
at edu.udel.rapp.rapp.rappApplication.run(rappApplication.java:1)
at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:85)
at io.dropwizard.cli.Cli.run(Cli.java:75)
at io.dropwizard.Application.run(Application.java:79)
at edu.udel.rapp.rapp.rappApplication.main(rappApplication.java:31)

在本类(class)中:

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

import org.skife.jdbi.v2.DBI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.client.JerseyClientBuilder;

import io.dropwizard.jdbi.DBIFactory;
import edu.udel.rapp.resources.ClientResource;
import edu.udel.rapp.resources.StudentResource;

import com.sun.jersey.api.client.Client;



public class rappApplication extends Application<rappConfiguration> {
private static final Logger LOGGER =
LoggerFactory.getLogger(rappApplication.class);

public static void main(final String[] args) throws Exception {
new rappApplication().run(args);
}

@Override
public String getName() {
return "rapp";
}

@Override
public void initialize(final Bootstrap<rappConfiguration> bootstrap) {
// TODO: application initialization
}




@Override
public void run(rappConfiguration c, Environment e) throws Exception {

final Client client = new JerseyClientBuilder(e).build("REST Client");
e.jersey().register(new ClientResource(client));


LOGGER.info("Method App#run() called");
for (int i=0; i < c.getMessageRepetitions(); i++) {
System.out.println(c.getMessage());
}
System.out.println(c.getAdditionalMessage());
// Create a DBI factory and build a JDBI instance
final DBIFactory factory = new DBIFactory();
final DBI jdbi = factory.build(e, c.getDataSourceFactory(), "mysql");
// Add the resource to the environment
e.jersey().register(new StudentResource(jdbi, e.getValidator()));
}






}

现在在书中,我的 run 方法中的客户端初始化不需要需要转换为 Client,但 Eclipse 给了我一个编译错误(“类型不匹配:无法从 javax.ws 转换。 rs.client.Client 到 com.sun.jersey.api.client.Client") 如果我不在那里转换为 Client...这会导致运行时错误。

另外,我很困惑为什么当我为类导入的是 io.dropwizard.client.JerseyClientBuilder 时 Eclipse 会出现“org.glassfish.jersey.client.JerseyClient”问题;

这几乎就像 Eclipse 对客户端实际上是什么感到困惑。

编辑:我在 pom.xml 中使用 Maven 并具有以下依赖项,

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.7</version>
</dependency>

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<version>${dropwizard.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-client</artifactId>
<version>${dropwizard.version}</version>
</dependency>

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-assets</artifactId>
<version>${dropwizard.version}</version>
</dependency>


<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-forms</artifactId>
<version>${dropwizard.version}</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.190</version>
</dependency>

<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.22.0</version>
</dependency>

<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.22.0</version>
</dependency>

<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-drive</artifactId>
<version>v3-rev32-1.22.0</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-jdbi</artifactId>
<version>${dropwizard.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-core -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.8</version>
</dependency>




</dependencies>

执行 mvn clean dependency:tree 给出以下输出

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building rapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ rapp ---
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ rapp ---
[INFO] edu.udel.rapp:rapp:jar:0.0.1-SNAPSHOT
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.6.7:compile
[INFO] +- io.dropwizard:dropwizard-testing:jar:1.0.4:test
[INFO] | +- io.dropwizard:dropwizard-core:jar:1.0.4:compile
[INFO] | | +- io.dropwizard:dropwizard-util:jar:1.0.4:compile
[INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.7.8:compile
[INFO] | | | +- com.google.guava:guava:jar:19.0:compile
[INFO] | | | \- joda-time:joda-time:jar:2.9.4:compile
[INFO] | | +- io.dropwizard:dropwizard-jackson:jar:1.0.4:compile
[INFO] | | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.7.8:compile
[INFO] | | | +- com.fasterxml.jackson.datatype:jackson-datatype-guava:jar:2.7.8:compile
[INFO] | | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.7.8:compile
[INFO] | | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.7.8:compile
[INFO] | | | +- com.fasterxml.jackson.module:jackson-module-afterburner:jar:2.7.8:compile
[INFO] | | | \- com.fasterxml.jackson.datatype:jackson-datatype-joda:jar:2.7.8:compile
[INFO] | | +- io.dropwizard:dropwizard-validation:jar:1.0.4:compile
[INFO] | | | +- org.hibernate:hibernate-validator:jar:5.2.4.Final:compile
[INFO] | | | | +- org.jboss.logging:jboss-logging:jar:3.2.1.Final:compile
[INFO] | | | | \- com.fasterxml:classmate:jar:1.1.0:compile
[INFO] | | | \- org.glassfish:javax.el:jar:3.0.0:compile
[INFO] | | +- io.dropwizard:dropwizard-configuration:jar:1.0.4:compile
[INFO] | | | +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.7.8:compile
[INFO] | | | | \- org.yaml:snakeyaml:jar:1.15:compile
[INFO] | | | \- org.apache.commons:commons-lang3:jar:3.4:compile
[INFO] | | +- io.dropwizard:dropwizard-logging:jar:1.0.4:compile
[INFO] | | | +- io.dropwizard.metrics:metrics-logback:jar:3.1.2:compile
[INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.21:compile
[INFO] | | | +- ch.qos.logback:logback-core:jar:1.1.7:compile
[INFO] | | | +- org.slf4j:log4j-over-slf4j:jar:1.7.21:compile
[INFO] | | | +- org.slf4j:jcl-over-slf4j:jar:1.7.21:compile
[INFO] | | | \- org.eclipse.jetty:jetty-util:jar:9.3.9.v20160517:compile
[INFO] | | +- io.dropwizard:dropwizard-metrics:jar:1.0.4:compile
[INFO] | | +- io.dropwizard:dropwizard-jersey:jar:1.0.4:compile
[INFO] | | | +- org.glassfish.jersey.ext:jersey-metainf-services:jar:2.23.2:compile
[INFO] | | | +- org.glassfish.jersey.ext:jersey-bean-validation:jar:2.23.2:compile
[INFO] | | | +- io.dropwizard.metrics:metrics-jersey2:jar:3.1.2:compile
[INFO] | | | +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.7.8:compile
[INFO] | | | | +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.7.8:compile
[INFO] | | | | \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.7.8:compile
[INFO] | | | +- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.23.2:compile
[INFO] | | | +- org.eclipse.jetty:jetty-server:jar:9.3.9.v20160517:compile
[INFO] | | | | +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] | | | | \- org.eclipse.jetty:jetty-io:jar:9.3.9.v20160517:compile
[INFO] | | | +- org.eclipse.jetty:jetty-webapp:jar:9.3.9.v20160517:compile
[INFO] | | | | \- org.eclipse.jetty:jetty-xml:jar:9.3.9.v20160517:compile
[INFO] | | | \- org.eclipse.jetty:jetty-continuation:jar:9.3.9.v20160517:compile
[INFO] | | +- io.dropwizard:dropwizard-jetty:jar:1.0.4:compile
[INFO] | | | +- io.dropwizard.metrics:metrics-jetty9:jar:3.1.2:compile
[INFO] | | | +- org.eclipse.jetty:jetty-servlet:jar:9.3.9.v20160517:compile
[INFO] | | | | \- org.eclipse.jetty:jetty-security:jar:9.3.9.v20160517:compile
[INFO] | | | +- org.eclipse.jetty:jetty-servlets:jar:9.3.9.v20160517:compile
[INFO] | | | \- org.eclipse.jetty:jetty-http:jar:9.3.9.v20160517:compile
[INFO] | | +- io.dropwizard:dropwizard-lifecycle:jar:1.0.4:compile
[INFO] | | +- io.dropwizard.metrics:metrics-core:jar:3.1.2:compile
[INFO] | | +- io.dropwizard.metrics:metrics-jvm:jar:3.1.2:compile
[INFO] | | +- io.dropwizard.metrics:metrics-servlets:jar:3.1.2:compile
[INFO] | | | \- io.dropwizard.metrics:metrics-json:jar:3.1.2:compile
[INFO] | | +- io.dropwizard.metrics:metrics-healthchecks:jar:3.1.2:compile
[INFO] | | +- io.dropwizard:dropwizard-request-logging:jar:1.0.4:compile
[INFO] | | | \- ch.qos.logback:logback-access:jar:1.1.7:compile
[INFO] | | +- net.sourceforge.argparse4j:argparse4j:jar:0.7.0:compile
[INFO] | | \- org.eclipse.jetty.toolchain.setuid:jetty-setuid-java:jar:1.0.3:compile
[INFO] | +- org.mockito:mockito-core:jar:2.0.54-beta:test
[INFO] | | \- net.bytebuddy:byte-buddy:jar:1.3.16:test
[INFO] | +- org.objenesis:objenesis:jar:2.3:test
[INFO] | +- org.assertj:assertj-core:jar:3.4.1:test
[INFO] | \- org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-inmemory:jar:2.23.2:test
[INFO] | +- org.glassfish.jersey.test-framework:jersey-test-framework-core:jar:2.23.2:test
[INFO] | | \- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.23.2:compile
[INFO] | \- org.ow2.asm:asm-debug-all:jar:5.0.4:test
[INFO] +- io.dropwizard:dropwizard-client:jar:1.0.4:compile
[INFO] | +- org.glassfish.jersey.core:jersey-client:jar:2.23.2:compile
[INFO] | +- org.apache.httpcomponents:httpclient:jar:4.5.2:compile
[INFO] | | +- org.apache.httpcomponents:httpcore:jar:4.4.4:compile
[INFO] | | \- commons-codec:commons-codec:jar:1.9:compile
[INFO] | +- io.dropwizard.metrics:metrics-httpclient:jar:3.1.2:compile
[INFO] | \- org.glassfish.jersey.connectors:jersey-apache-connector:jar:2.23.2:compile
[INFO] +- io.dropwizard:dropwizard-assets:jar:1.0.4:compile
[INFO] | \- io.dropwizard:dropwizard-servlets:jar:1.0.4:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.7.21:compile
[INFO] | +- io.dropwizard.metrics:metrics-annotation:jar:3.1.2:compile
[INFO] | \- ch.qos.logback:logback-classic:jar:1.1.7:compile
[INFO] +- io.dropwizard:dropwizard-forms:jar:1.0.4:compile
[INFO] | \- org.glassfish.jersey.media:jersey-media-multipart:jar:2.23.2:compile
[INFO] | \- org.jvnet.mimepull:mimepull:jar:1.9.6:compile
[INFO] +- com.h2database:h2:jar:1.4.190:compile
[INFO] +- com.google.api-client:google-api-client:jar:1.22.0:compile
[INFO] | +- com.google.oauth-client:google-oauth-client:jar:1.22.0:compile
[INFO] | | +- com.google.http-client:google-http-client:jar:1.22.0:compile
[INFO] | | \- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] | +- com.google.http-client:google-http-client-jackson2:jar:1.22.0:compile
[INFO] | \- com.google.guava:guava-jdk5:jar:17.0:compile
[INFO] +- com.google.oauth-client:google-oauth-client-jetty:jar:1.22.0:compile
[INFO] | +- com.google.oauth-client:google-oauth-client-java6:jar:1.22.0:compile
[INFO] | \- org.mortbay.jetty:jetty:jar:6.1.26:compile
[INFO] | +- org.mortbay.jetty:jetty-util:jar:6.1.26:compile
[INFO] | \- org.mortbay.jetty:servlet-api:jar:2.5-20081211:compile
[INFO] +- com.google.apis:google-api-services-drive:jar:v3-rev32-1.22.0:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.6:compile
[INFO] +- io.dropwizard:dropwizard-jdbi:jar:1.0.4:compile
[INFO] | +- io.dropwizard:dropwizard-db:jar:1.0.4:compile
[INFO] | | \- org.apache.tomcat:tomcat-jdbc:jar:8.5.3:compile
[INFO] | | \- org.apache.tomcat:tomcat-juli:jar:8.5.3:compile
[INFO] | +- org.jdbi:jdbi:jar:2.73:compile
[INFO] | \- io.dropwizard.metrics:metrics-jdbi:jar:3.1.2:compile
[INFO] +- org.glassfish.jersey.core:jersey-server:jar:2.11:compile
[INFO] | +- org.glassfish.jersey.core:jersey-common:jar:2.11:compile
[INFO] | | +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.11:compile
[INFO] | | \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
[INFO] | +- javax.ws.rs:javax.ws.rs-api:jar:2.0:compile
[INFO] | +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO] | +- org.glassfish.hk2:hk2-api:jar:2.3.0-b05:compile
[INFO] | | +- org.glassfish.hk2:hk2-utils:jar:2.3.0-b05:compile
[INFO] | | \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.3.0-b05:compile
[INFO] | +- org.glassfish.hk2.external:javax.inject:jar:2.3.0-b05:compile
[INFO] | +- org.glassfish.hk2:hk2-locator:jar:2.3.0-b05:compile
[INFO] | | \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] | \- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] \- com.sun.jersey:jersey-core:jar:1.8:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

最佳答案

如代码所示here ,

new JerseyClientBuilder(e).build("REST Client");

返回javax.ws.rs.client.Client类型的实例。但您已在代码中导入了 com.sun.jersey.api.client.Client

替换

import com.sun.jersey.api.client.Client;

import javax.ws.rs.client.Client

关于java - Dropwizard 客户端类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41542701/

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