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