Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Can I have the same service having simultaneously both REST and SOAP interfaces? I currently have a REST service implemented in Java using EJB and Jersey:

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?

like image 897
Daniel Duarte Figueiredo Avatar asked Jun 06 '12 13:06

Daniel Duarte Figueiredo


2 Answers

Basically, Jersey is JAX-RS implementation, so you cannot have SOAP web-services here. 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.

like image 155
Alex Stybaev Avatar answered Oct 08 '22 19:10

Alex Stybaev


Here is a solution to expose an implementation as both rest and soap web service. Similar to what zack suggested in the comment. You may have to do some refactoring if you already have the service supporting jax-rs as you pasted above.

The solution is to have two sets of interfaces and implementation. One supporting jax-rs and one jax-ws. You can still have your processing done in the ejb.

Example,

Do not annotate your ejb (say EService) with jax-rs.

Have an interface X and Ximpl class to support restful calls. This will support jax-rs, so basically be annotated with jax-rs. Ofcourse, this can still use jersey. Ximpl will reference the EJB EService and delegate the processing to it.

Have an interface Y and YImpl to support soap based calls. This will support jax-ws, so will be annotated with jax-ws. Yimpl will reference the EJB EService and delegate the processing to it.

If you have a web deployment descriptor, in your web deployment descriptor define different servlets and mapping for rest and soap.

like image 40
techuser soma Avatar answered Oct 08 '22 18:10

techuser soma