简体   繁体   English

使用Java / Jersey作为REST和SOAP的Web服务

[英]Web service that works as REST and SOAP using Java/Jersey

Can I have the same service having simultaneously both REST and SOAP interfaces? 我是否可以同时拥有REST和SOAP接口的服务? I currently have a REST service implemented in Java using EJB and Jersey: 我目前使用EJB和Jersey在Java中实现了REST服务:

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;  

@Stateless
@Path("test")
public class TestExternalService {

    @EJB
    private com.test.ejb.db.TestService testService;

    @GET
    @Path("/status")
    @Produces("text/*")
    public String status() {
        return "ok";
    }
}

How can I make changes in my class to also implement a SOAP interface? 如何在我的类中进行更改以实现SOAP接口?

Basically, Jersey is JAX-RS implementation, so you cannot have SOAP web-services here. 基本上, JerseyJAX-RS实现,因此您不能在此处拥有SOAP Web服务。 You could take Apache CXF , which is implementation for both JAX-RS and JAX-WS and you would be able to combine your web-services in both architectural styles. 你可以使用Apache CXF ,它是JAX-RSJAX-WS ,你可以在两种架构风格中组合你的web服务。

Here is a solution to expose an implementation as both rest and soap web service. 这是一个将实现公开为rest和soap web服务的解决方案。 Similar to what zack suggested in the comment. 与zack在评论中提出的相似。 You may have to do some refactoring if you already have the service supporting jax-rs as you pasted above. 如果您已经拥有支持jax-rs的服务,则可能需要进行一些重构。

The solution is to have two sets of interfaces and implementation. 解决方案是拥有两组接口和实现。 One supporting jax-rs and one jax-ws. 一个支持jax-rs和一个jax-ws。 You can still have your processing done in the ejb. 您仍然可以在ejb中完成处理。

Example, 例,

Do not annotate your ejb (say EService) with jax-rs. 不要用jax-rs注释你的ejb(比如EService)。

Have an interface X and Ximpl class to support restful calls. 有一个接口X和Ximpl类来支持restful调用。 This will support jax-rs, so basically be annotated with jax-rs. 这将支持jax-rs,所以基本上用jax-rs注释。 Ofcourse, this can still use jersey. 当然,这仍然可以使用球衣。 Ximpl will reference the EJB EService and delegate the processing to it. Ximpl将引用EJB EService并将处理委托给它。

Have an interface Y and YImpl to support soap based calls. 有一个接口Y和YImpl来支持基于soap的呼叫。 This will support jax-ws, so will be annotated with jax-ws. 这将支持jax-ws,因此将使用jax-ws进行注释。 Yimpl will reference the EJB EService and delegate the processing to it. Yimpl将引用EJB EService并将处理委托给它。

If you have a web deployment descriptor, in your web deployment descriptor define different servlets and mapping for rest and soap. 如果您有Web部署描述符,则在Web部署描述符中定义不同的servlet并映射rest和soap。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM