Is it possible (Java EE, JBoss 6) to cleverly annotate Java code to provide both RESTful and SOAP webservices without implementing two methods?
I'm thinking about:
@Local
@Path("/service")
@WebService
public interface SomeService {
@GET @Path("somemethod")
@WebMethod
public String someMethod (@QueryParam("s") String someParam);
}
Please notice both @Path and @WebService annotations (above example does not work unfortunately).
While JAX-WS and JAX-RS annotations can quite happily cohabit on the same methods, I find it hard to believe that any interface that is well-tuned for use with JAX-RS could be a good fit for JAX-WS, or vice versa. The problem isn't that you can't do it, the problem is that you shouldn't; they have a different model of the world, a different concept of what it means to be a good interface.
But if you're just doing something trivial like a simple lookup, it can indeed work:
@GET
@Path("foo/{id}")
@Produces("application/xml")
@WebMethod(operationName = "DescribeFoo")
@WebResult(name = "Description")
public DescriptionOfFoo getFooDescription(
@PathParam("id")
@WebParam(name = "fooId")
String id) {
return get_the.description_of(id); // Whatever...
}
I like to put as much as I can on interfaces though (check your framework documentation for how to make them work) as that reduces the above partial (!!) set of possible annotations down to a more sensible level. (For reference, when things start to get complicated and you're applying multiple aspects in non-trivial patterns, you can easily get to over 20 annotations per method, some of which relate to your implementation and some of which relate to one or other of your interfaces; partitioning promotes sanity.)
I would expect to apply the JAX/RS annotations to a Class not an Interface.
You may also need to do some configuring to add the JAX/RS servlet implementation to your web app.
I've got some more detailed explanation here, it's in WebSphere not JBoss, but it's using an Apache open source implementation so may well be relevant.
In concept I don't see why the same simple method couldn't be exposed as both Web service and RESTful service, however, the design philosophies of the two approaches are fundamentally different, when you get to anything non-trivial I just don't this will mesh.