SOAP 和 REST Web 服务与 Java EE 中的一个实现 [英] SOAP and REST Webservice with one implementation in Java EE

查看:21
本文介绍了SOAP 和 REST Web 服务与 Java EE 中的一个实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能(Java EE、JBoss 6)巧妙地注释 Java 代码以提供 RESTful 和 SOAP 网络服务,而无需实现两种方法?

Is it possible (Java EE, JBoss 6) to cleverly annotate Java code to provide both RESTful and SOAP webservices without implementing two methods?

我在考虑:

@Local
@Path("/service")
@WebService
public interface SomeService {
    @GET @Path("somemethod")
    @WebMethod
    public String someMethod (@QueryParam("s") String someParam);
}

请注意@Path@WebService 注释(不幸的是,上面的示例不起作用).

Please notice both @Path and @WebService annotations (above example does not work unfortunately).

推荐答案

虽然 JAX-WS 和 JAX-RS 注释可以很愉快地共存于相同的方法上,但我发现很难相信任何针对与 JAX-RS 一起使用可能非常适合 JAX-WS,或者反之亦然.问题不在于你做不到,问题在于你不应该;他们对世界有不同的模型,对于一个好的界面意味着什么有着不同的概念.

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

我喜欢在接口上尽可能多地投入(检查您的框架文档以了解如何使它们工作),因为这将上述部分(!!)可能的注释集减少到更明智的水平.(作为参考,当事情开始变得复杂并且您在非平凡模式中应用多个方面时,您可以轻松地获得每个方法的 20 多个注释,其中一些与您的实现有关,其中一些与一个或其他您的界面;分区促进了理智.)

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

这篇关于SOAP 和 REST Web 服务与 Java EE 中的一个实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆