Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java DropWizard/Jersey API客户端_Java_Rest_Jersey_Dropwizard_Webservices Client - Fatal编程技术网

Java DropWizard/Jersey API客户端

Java DropWizard/Jersey API客户端,java,rest,jersey,dropwizard,webservices-client,Java,Rest,Jersey,Dropwizard,Webservices Client,在引擎盖下休息。我正试图找出如何为DropWizard应用程序将公开的RESTful端点编写客户端 在本例中,假设我的DropWizard应用程序有一个CarResource,它公开了一些简单的RESTful端点,用于加固汽车: @Path("/cars") public class CarResource extends Resource { // CRUDs car instances to some database (DAO). public CardDao carDao

在引擎盖下休息。我正试图找出如何为DropWizard应用程序将公开的RESTful端点编写客户端

在本例中,假设我的DropWizard应用程序有一个
CarResource
,它公开了一些简单的RESTful端点,用于加固汽车:

@Path("/cars")
public class CarResource extends Resource {
    // CRUDs car instances to some database (DAO).
    public CardDao carDao = new CarDao();

    @POST
    public Car createCar(String make, String model, String rgbColor) {
        Car car = new Car(make, model, rgbColor);
        carDao.saveCar(car);

        return car;
    }

    @GET
    @Path("/make/{make}")
    public List<Car> getCarsByMake(String make) {
        List<Car> cars = carDao.getCarsByMake(make);
        return cars;
    }
}
但是,我能找到的关于Drop Wizard客户端的两个官方参考完全相互矛盾:

  • -要求我将客户代码放入
    car.service.client
    包下的
    car-client
    项目中;但是
  • -这让它看起来像“DropWizard客户端”是为了将我的DropWizard应用程序与其他RESTful web服务集成在一起(从而充当中间人)

所以我问,为DropWizard web服务编写Java API客户端的标准方法是什么?DropWizard是否有一个客户端库可用于这种类型的用例?我应该通过一些Jersey客户端API实现客户端吗?是否有人可以将伪代码添加到我的
CarServiceClient
,这样我就可以理解它是如何工作的了?

您可以与Spring框架集成以实现是的,dropwizard客户端提供的功能只供服务本身使用,最有可能用于通信其他服务。它不直接为客户端应用程序提供任何东西

不管怎样,它对HttpClients没有多大的魔力。它只是根据yml文件配置客户机,将现有的Jackson对象映射器和验证器分配给Jersey客户机,并且我认为重用了应用程序的线程池。你可以检查一下

我想我会像你们使用Jersey客户端一样去构建我的类。以下是我用于客户端服务的抽象类:

public abstract class HttpRemoteService {

  private static final String AUTHORIZATION_HEADER = "Authorization";
  private static final String TOKEN_PREFIX = "Bearer ";

  private Client client;

  protected HttpRemoteService(Client client) {
    this.client = client;
  }

  protected abstract String getServiceUrl();  

  protected WebResource.Builder getSynchronousResource(String resourceUri) {
    return client.resource(getServiceUrl() + resourceUri).type(MediaType.APPLICATION_JSON_TYPE);
  }

  protected WebResource.Builder getSynchronousResource(String resourceUri, String authToken) {
    return getSynchronousResource(resourceUri).header(AUTHORIZATION_HEADER, TOKEN_PREFIX + authToken);
  }

  protected AsyncWebResource.Builder getAsynchronousResource(String resourceUri) {
    return client.asyncResource(getServiceUrl() + resourceUri).type(MediaType.APPLICATION_JSON_TYPE);
  }

  protected AsyncWebResource.Builder getAsynchronousResource(String resourceUri, String authToken) {
    return getAsynchronousResource(resourceUri).header(AUTHORIZATION_HEADER, TOKEN_PREFIX + authToken);
  }

  protected void isAlive() {
    client.resource(getServiceUrl()).get(ClientResponse.class);
  }  

}
以下是我如何使其具体化:

private class TestRemoteService extends HttpRemoteService {

    protected TestRemoteService(Client client) {
      super(client);
    }

    @Override
    protected String getServiceUrl() {
      return "http://localhost:8080";
    }

    public Future<TestDTO> get() {
      return getAsynchronousResource("/get").get(TestDTO.class);
    }

    public void post(Object object) {
      getSynchronousResource("/post").post(object);
    }

    public void unavailable() {
      getSynchronousResource("/unavailable").get(Object.class);
    }

    public void authorize() {
      getSynchronousResource("/authorize", "ma token").put();
    }
  }
私有类TestRemoteService扩展了HttpRemoteService{
受保护的TestRemoteService(客户端){
超级(客户);
}
@凌驾
受保护的字符串getServiceUrl(){
返回“http://localhost:8080";
}
公共未来获取(){
返回getAsynchronousResource(“/get”).get(TestDTO.class);
}
公共作废帖子(对象){
getSynchronousResource(“/post”).post(对象);
}
公共空间不可用(){
getSynchronousResource(“/unavailable”).get(Object.class);
}
公开授权{
getSynchronousResource(“/authorize”,“ma令牌”).put();
}
}

如果有人在构建客户端时试图使用DW 0.8.2,您会收到以下错误:

cannot access org.apache.http.config.Registry
class file for org.apache.http.config.Registry not found

at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:858)
at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
... 19 more
将pom.xml中的dropwizard客户端从0.8.2更新到0.8。4,您应该很好。我相信jetty子依赖项已经更新,修复了它

    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-client</artifactId>
        <version>0.8.4</version>
        <scope>compile</scope>
    </dependency>

io.dropwizard
dropwizard客户端
0.8.4
编译

下面是一个可以使用JAX-RS客户端的模式

要获取客户端,请执行以下操作:

javax.ws.rs.client.Client init(JerseyClientConfiguration config, Environment environment) {
    return new JerseyClientBuilder(environment).using(config).build("my-client");
}
然后,您可以通过以下方式拨打电话:

javax.ws.rs.core.Response post = client
        .target("http://...")
        .request(MediaType.APPLICATION_JSON)
        .header("key", value)
        .accept(MediaType.APPLICATION_JSON)
        .post(Entity.json(myObj));
javax.ws.rs.core.Response post = client
        .target("http://...")
        .request(MediaType.APPLICATION_JSON)
        .header("key", value)
        .accept(MediaType.APPLICATION_JSON)
        .post(Entity.json(myObj));