gpt4 book ai didi

java - 您必须至少使用一个,但不超过一个 http 方法注释,用于 reaseasy 代理客户端

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

我正在尝试轻松实现一个简单的客户端,但我收到一条错误消息“您必须至少使用一个,但不超过一个 http 方法注释”。在我的服务器实现中,我在我的方法上添加了一个 http 注释。

    @Path("/")
public class TestResource
{
@GET
@Path("/domain/{value}")
public String get(@PathParam("value") final String value) {
return "Hello" + value;
}
}

我调试了它,第一次它没有遇到运行时异常,但是,它第二次调用它并失败了,不知道为什么以及如何。

我的客户端作为 junit 测试:

@Test
public void testPerformRestEasy() {

ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://localhost:8080/");
TestResource proxy = target.proxy(TestResource.class);
String response = proxy.get("user");
Assert.assertEquals("Hellouser", response);
}

失败的代码

    private static <T> ClientInvoker createClientInvoker(Class<T> clazz, Method method, ResteasyWebTarget base, ProxyConfig config)
{
Set<String> httpMethods = IsHttpMethod.getHttpMethods(method);
if (httpMethods == null || httpMethods.size() != 1)
{
throw new RuntimeException("You must use at least one, but no more than one http method annotation on: " + method.toString());
}
ClientInvoker invoker = new ClientInvoker(base, clazz, method, config);
invoker.setHttpMethod(httpMethods.iterator().next());
return invoker;
}

错误:

java.lang.RuntimeException: You must use at least one, but no more than one http method annotation on: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
at org.jboss.resteasy.client.jaxrs.ProxyBuilder.createClientInvoker(ProxyBuilder.java:76)
at org.jboss.resteasy.client.jaxrs.ProxyBuilder.proxy(ProxyBuilder.java:52)
at org.jboss.resteasy.client.jaxrs.ProxyBuilder.build(ProxyBuilder.java:120)
at org.jboss.resteasy.client.jaxrs.internal.ClientWebTarget.proxy(ClientWebTarget.java:72)

有谁知道这里的问题是什么?

最佳答案

Resteasy JAXRS 2 客户端似乎不直接接受实现类。要使其工作,您必须创建一个正确注释的界面。 Resteasy 使用它来生成客户端代理,您的服务器必须实现完全相同的接口(interface)。

因此在您的情况下,您必须将代码拆分为一个接口(interface)和一个单独的实现类:

@Path("/")
public interface TestResource {
@GET
@Path("/domain/{value}")
String get(@PathParam("value") final String value);
}

public class TestResourceImpl implements TestResource {
@Override String get(final String value) {
return "Hello" + value;
}
}

我不确定这是 Resteasy 特有的还是规范要求的,但为我解决了同样的问题。您可以找到给我提示的部分 here in the documentation .

关于java - 您必须至少使用一个,但不超过一个 http 方法注释,用于 reaseasy 代理客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29762619/

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