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

Lif*_*rld 8 java rest web-services resteasy

我试图在休息时轻松实现一个简单的客户端,但我收到一个错误,说"你必须使用至少一个,但不超过一个http方法注释".在我的服务器实现中,我在我的方法上添加了一个http注释.

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

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

我的客户作为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);
}
Run Code Online (Sandbox Code Playgroud)

它失败的代码

    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;
   }
Run Code Online (Sandbox Code Playgroud)

错误:

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)
Run Code Online (Sandbox Code Playgroud)

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

Sai*_*eek 1

您必须@Produces/@Consumes从客户端定义resource() 的MIME 媒体类型资源表示。喜欢 -

@Path("/")
public class TestResource
{
    @GET
    @Produces("text/plain")
    @Path("/domain/{value}")
    public String get(@PathParam("value") final String value) {
        return "Hello" + value;
    }
} 
Run Code Online (Sandbox Code Playgroud)

Jboss客户端框架文档将为您提供更多帮助。

  • 我按照您所说添加了生成注释,但这不起作用。**@Produces(“文本/纯文本”)** (2认同)