Java中常见的webservice实现,SOAP和REST Webservice,在Java EE中有一个实现

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值