Java 您必须在for reaseasy代理客户端上至少使用一个http方法批注,但不能超过一个
我试图在rest easy中实现一个简单的客户机,但是我收到一个错误,上面说“您必须使用至少一个,但不能超过一个http方法注释”。在我的服务器实现中,我在方法上添加了一个http注释Java 您必须在for reaseasy代理客户端上至少使用一个http方法批注,但不能超过一个,java,web-services,rest,resteasy,Java,Web Services,Rest,Resteasy,我试图在rest easy中实现一个简单的客户机,但是我收到一个错误,上面说“您必须使用至少一个,但不能超过一个http方法注释”。在我的服务器实现中,我在方法上添加了一个http注释 @Path("/") public class TestResource { @GET @Path("/domain/{value}") public String get(@PathParam("value") final String value) { retu
@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;
}
有人知道这是什么问题吗?您必须定义客户端资源(
@products/@Consumes
)的MIME媒体类型资源表示。像-
@Path("/")
public class TestResource
{
@GET
@Produces("text/plain")
@Path("/domain/{value}")
public String get(@PathParam("value") final String value) {
return "Hello" + value;
}
}
将为您提供更多帮助。Resteasy JAXRS 2客户端似乎不直接接受实现类。要使其工作,您必须创建一个正确注释的接口。Resteasy使用它来生成客户机代理,您的服务器必须实现完全相同的接口 因此,在您的情况下,您必须将代码拆分为一个接口和一个单独的实现类:
@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特定的还是规范要求的,但为我解决了同样的问题。您可以找到给我提示的部分。在我的例子中,Rest客户端接口的开发人员错误地扩展了
RestEasyClientProxy
。缺少http注释的不是Rest接口中的方法,而是继承的方法
从Rest客户端接口代码中删除
扩展RestEasyClientProxy
修复了此问题。您仍然面临此问题吗?是。我刚刚在我的资源文件中添加了products注释,但没有效果。有人知道如何修复它吗?我可以采用的另一种方法是resteasy和apache客户端,而不是reasteasy代理。我很乐意使用resteasy代理。@user3369719:您解决了这个问题吗?我也面临同样的问题。你尝试过下面提到的解决方案吗?我使用了一个不同的客户端实现。正如您所说,我添加了生成注释,但这不起作用@生成(“文本/纯文本”)
@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;
}
}