5

I am trying to call a webservice method via a proxy but I have got an error message that says: "Subresource for target class has no jax-rs annotations.: org.jboss.resteasy.core.ServerResponse"

Here is my server class

@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;


    }
}

with a an interface like this

@Path("/authorizationCheck")
public interface AuthorizationService {

    @POST
    @Path("/webserviceTest")
    public Response webserviceTest();
}

and my return object wrapped in response

@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;
    }



}

and finally my client class

@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;



    }
}

what I am doing wrong here , any help will be appreciated.

Yashar
  • 1,122
  • 2
  • 15
  • 43

3 Answers3

4

You have two annotations with webserviceTest() which are @POST and @Path.

Repeat BOTH the annotations in over ridden method in implemented class. That means add the @POST annotation to webserviceTest() method.

It should work then !

And here is the reason why it din't work.. without proper annotations in implementing class. Why java classes do not inherit annotations from implemented interfaces?

Community
  • 1
  • 1
Rakesh Waghela
  • 2,227
  • 2
  • 26
  • 46
  • I tried what you have mentioned (put all annotation in both interface and implementation class) but nothing changed. I have still the same exception – Yashar Sep 26 '13 at 07:37
2

You can remove the @Path annotations on the implementation class and concrete method, and only annotate your interfaces, like this:

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;
    }
}

Note: don't forget @Produces on your interface method to define your MIME type, such as MediaType.APPLICATION_XML

@Path("/authorizationCheck")
public interface AuthorizationService {

    @POST
    @Path("/webserviceTest")
    @Produces(MediaType.APPLICATION_XML)
    public Response webserviceTest();
}

See an example here: 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

and here: 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

0

I changed like this

@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;


}
}

My Test Client is different

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;
}
}

It produced output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><testDTO><fieldOne>ffff</fieldOne><fieldTwo>gggg</fieldTwo></testDTO>

Had to add following dependency also

<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>

Tried your code.

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;
}

Produced same output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><testDTO><fieldOne>ffff</fieldOne><fieldTwo>gggg</fieldTwo></testDTO>

Note how I created proxy. I had only base url **http://localhost:9095/**. I did not mention resources authorizationCheck/webserviceTest in that. This is different from how you coded.

Satish
  • 713
  • 1
  • 5
  • 18
  • Hi Satish, I want to use proxy since it make it easier to send Object parameter along with web service call. (like this proxy.webserviceTest(testDTO) ), but it doesn't work even after your suggested change – Yashar Sep 30 '13 at 12:51
  • Your first suggestion is working but it is not what i am looking for. I want to send a complicated object as a parameter with my webservice call which seems impossible with your first suggestion. I want to focus on the proxy solution since it make it easier to send objects as webservice parameters – Yashar Sep 30 '13 at 13:00
  • I would use JSON. GSon library `http://code.google.com/p/google-gson/` is a good library which converts Objects to JSON. Once in JSON you can pass the string. At the receiving end you can convert from JSON string to Objects using GSon again. – Satish Oct 04 '13 at 09:05
  • Try this link http://www.vogella.com/articles/REST/article.html. Check if helps. – Satish Oct 07 '13 at 14:44