我试图通过代理调用webservice方法,但我收到一条错误消息:“目标类的子资源没有jax-rs注释:org.jboss.resteasy.core.ServerResponse”
这是我的服务器类
@Path("/authorizationCheck")
public class AuthorizationRestService implements AuthorizationService {
@Override
@Path("/webserviceTest")
public Response webserviceTest(){
TestDTO x = new TestDTO();
x.setFieldOne("ffff");
x.setFieldTwo("gggg");
Response res = Response.ok(x).build();
return res;
}
}
使用像这样的界面
@Path("/authorizationCheck")
public interface AuthorizationService {
@POST
@Path("/webserviceTest")
public Response webserviceTest();
}
并将我的返回对象包装在响应中
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class TestDTO {
private String fieldOne;
private String fieldTwo;
public String getFieldOne() {
return fieldOne;
}
public void setFieldOne(String fieldOne) {
this.fieldOne = fieldOne;
}
public String getFieldTwo() {
return fieldTwo;
}
public void setFieldTwo(String fieldTwo) {
this.fieldTwo = fieldTwo;
}
}
最后是我的客户端类
@Stateful
@Scope(ScopeType.CONVERSATION)
@Name("authorizationCheckService")
public class AuthorizationCheckService {
public void testWebservice(){
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
AuthorizationService proxy =
ProxyFactory.create(AuthorizationService.class,
ApplicationConfig.WORKFLOWSERVER_URL + "services/authorizationCheck/webserviceTest");
Response response = proxy.webserviceTest();
return;
}
}
我在这里做错了,任何帮助都将不胜感激。
答案 0 :(得分:4)
您有两个带有webserviceTest()的注释,它们是@POST和@Path。
在实现的类中重复使用over ridden方法中的注释。这意味着将@POST注释添加到webserviceTest()方法。
它应该工作了!
这就是为什么它没有工作......没有适当的注释来实现类。 Why java classes do not inherit annotations from implemented interfaces?
答案 1 :(得分:2)
您可以删除实现类和具体方法上的@Path注释,并仅注释您的接口,如下所示:
public class AuthorizationRestService implements AuthorizationService {
@Override
public Response webserviceTest(){
TestDTO x = new TestDTO();
x.setFieldOne("ffff");
x.setFieldTwo("gggg");
Response res = Response.ok(x).build();
return res;
}
}
注意:请勿忘记界面方法上的@Produces来定义您的MIME类型,例如MediaType.APPLICATION_XML
@Path("/authorizationCheck")
public interface AuthorizationService {
@POST
@Path("/webserviceTest")
@Produces(MediaType.APPLICATION_XML)
public Response webserviceTest();
}
答案 2 :(得分:0)
我改变了这个
@Path("/authorizationCheck")
public class AuthorizationRestService implements AuthorizationService {
@Override
@Path("/webserviceTest")
@POST
@Produces(MediaType.APPLICATION_XML)
public Response webserviceTest(){
TestDTO x = new TestDTO();
x.setFieldOne("ffff");
x.setFieldTwo("gggg");
Response res = Response.ok(x).build();
return res;
}
}
我的测试客户端不同
public class CustomerResourceTest
{
@Test
public void testCustomerResource() throws Exception
{
URL postUrl = new URL("http://localhost:9095/authorizationCheck/webserviceTest");
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setRequestMethod("POST");
System.out.println("Content-Type: " + connection.getContentType());
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String line = reader.readLine();
while (line != null)
{
System.out.println(line);
line = reader.readLine();
}
Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
connection.disconnect();
return;
}
}
它产生了输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><testDTO><fieldOne>ffff</fieldOne><fieldTwo>gggg</fieldTwo></testDTO>
还必须添加以下依赖
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>2.2.0.GA</version>
</dependency>
尝试了你的代码。
public void testCustomerResource() throws Exception
{
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
AuthorizationService proxy =
ProxyFactory.create(AuthorizationService.class,"http://localhost:9095/");
ClientResponse response = (ClientResponse) proxy.webserviceTest();
String str = (String)response.getEntity(String.class);
System.out.println(str);
return;
}
生成相同的输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><testDTO><fieldOne>ffff</fieldOne><fieldTwo>gggg</fieldTwo></testDTO>
注意我是如何创建代理的。我只有基本网址**http://localhost:9095/**
。我没有提到资源 authorizationCheck / webserviceTest 。这与您编码的方式不同。