gpt4 book ai didi

java - 有没有办法将 JAX-RS 注释接口(interface)与 Jersey 作为客户端一起使用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:58 30 4
gpt4 key购买 nike

鉴于我有一个接口(interface)代表我的 RESET 服务使用

public interface BookResource {

@GET
@Path("/book/isbn/{isbn}/")
@Produces(value = { MediaType.APPLICATION_XML })
public ClientResponse<Book> getBookByIsbn(@PathParam("isbn") String isbn, @QueryParam("releaseStatus") String releaseStatus);

}

如果我需要在我的 web 应用程序中使用 Jersey 作为 JAX-RS 提供程序/REST 框架,我该如何创建一个实际服务实现的代理。

使用 RESTEasy/Spring 集成很容易做到这一点,这意味着我可以直接使用我的 JAX-RS 接口(interface),而无需包装它和正确的样板来执行调用。

基本上,我正在寻找等同于以下内容的 Jersey :-

<bean id="bookResource" class="org.jboss.resteasy.client.spring.RestClientProxyFactoryBean">
<property name="serviceInterface" value="my.company.book.service.BookResource" />
<property name="baseUri" value="http://localhost:8181/books-service/" />
</bean>

我刚刚花了最后一个小时在谷歌上搜索这个并继续回到 Jersey 中的标准客户端 API,这似乎需要大量的样板来实现相同的目的。谁能指出我正确的方向?

最佳答案

这个链接好像比较实用:http://blog.alutam.com/2012/05/04/proxy-client-on-top-of-jax-rs-2-0-client-api/

// configure Jersey client
ClientConfig cc = new ClientConfig().register(JacksonFeature.class)
.register(AnotherFeature.class)
.register(SomeFilter.class);
Client resource = ClientBuilder.newClient(cc);
// create client proxy
ServiceInterface proxy = WebResourceFactory.newResource(ServiceInterface.class,
resource.target(ServiceURI));

// invoke service
MyType result = proxy.someMethod();

对于 Maven 项目,您需要以下依赖项:

    <dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-proxy-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>

关于java - 有没有办法将 JAX-RS 注释接口(interface)与 Jersey 作为客户端一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17063789/

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