【问题标题】:Subresource for target class has no jax-rs annotations目标类的子资源没有 jax-rs 注释
【发布时间】:2013-09-25 08:40:34
【问题描述】:

我正在尝试通过代理调用 Web 服务方法,但我收到一条错误消息:“目标类的子资源没有 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;



    }
}

我在这里做错了什么,任何帮助将不胜感激。

【问题讨论】:

    标签: web-services rest jaxb jax-rs resteasy


    【解决方案1】:

    您有两个带有 webserviceTest() 的注释,它们是 @POST 和 @Path。

    在实现的类中重复覆盖方法中的两个注释。这意味着将 @POST 注释添加到 webserviceTest() 方法。

    那么它应该可以工作了!

    这就是为什么它不起作用的原因......在实现类中没有适当的注释。 Why java classes do not inherit annotations from implemented interfaces?

    【讨论】:

    • 我尝试了您提到的(将所有注释放在接口和实现类中),但没有任何改变。我仍然有同样的例外
    【解决方案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();
    }
    

    在此处查看示例:http://pic.dhe.ibm.com/infocenter/initiate/v9r5/index.jsp?topic=%2Fcom.ibm.composer.doc%2Ftopics%2Fr_composer_extending_services_creating_rest_service_rest_interface.html

    这里:http://pic.dhe.ibm.com/infocenter/initiate/v9r5/index.jsp?topic=%2Fcom.ibm.composer.doc%2Ftopics%2Fr_composer_extending_services_creating_rest_service_rest_interface.html

    【讨论】:

      【解决方案3】:

      我变成了这样

      @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。这与您的编码方式不同。

      【讨论】:

      • 嗨 Satish,我想使用代理,因为它可以更轻松地与 Web 服务调用一起发送对象参数。 (就像这个 proxy.webserviceTest(testDTO) ),但即使在您建议的更改之后它也不起作用
      • 您的第一个建议是有效的,但这不是我想要的。我想通过我的网络服务调用发送一个复杂的对象作为参数,这在您的第一个建议中似乎是不可能的。我想专注于代理解决方案,因为它可以更轻松地将对象作为 Web 服务参数发送
      • 我会使用 JSON。 GSon library http://code.google.com/p/google-gson/ 是一个很好的将 Objects 转换为 JSON 的库。在 JSON 中,您可以传递字符串。在接收端,您可以再次使用 GSon 将 JSON 字符串转换为对象。
      • 试试这个链接vogella.com/articles/REST/article.html。检查是否有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      相关资源
      最近更新 更多