beingsuplab
beingsuplab

Reputation: 165

Automating creation of Java Web Services from plain POJOs

The objective of what I'm trying to achieve is pretty simple (atleast I think so it is).

I have a plain java class containing say a method doAction(SomeObject obj). I want to expose it as a CXF webservice and its WSDL from this. A bottom-up approach will be followed in this case.

Now for a single class or two I might use Eclipse's Web Service Creation Wizard. But let's say I have 30-35 such cases, I was hoping to automate the process.

Any ideas on how to go about it?

Adding a little more info regarding the objective:

Lets say I have an interface,

public interface IProcessService {

public SomeObject doAction (SomeObject input) throws Exception;

}

and I have its concrete implementation

@Service
@ManagedResource
public class ProcessServiceImpl implements IProcessService {

public SomeObject doAction(SomeObject input) throws Exception{
//doSomething
}
}

Now I could probably parse the interface or the concrete service to generate the following service interface to be exposed as:

@WebService (targetnamespace="...", name="...", portname="...", serviceName="...")
public interface IExposedService{

@WebResult(name="output", targetnamespace="...")
@RequestWrapper(...)
@ResponseWrapper(...)
@WebMethod(action="...")
public SomeObject doAction( @WebParam(name="input") Someobject input) throws Exception;

}

My target is to generate the last interface pragmatically.

Upvotes: 0

Views: 57

Answers (1)

hinneLinks
hinneLinks

Reputation: 3736

You could use the jdk-tool wsgen. It requires, that your Webservice Class has the annotation @WebService.

Example:

Service:

package org.wstest;
import javax.jws.WebService;
@WebService
public class WsTest {
    public String doAction(MyObject o){
        String result = o.getText() + " - " + o.getNumber();
        return result;
    }
}

Object

package org.wstest;
import java.io.Serializable;
public class MyObject implements Serializable{
    private static final long serialVersionUID = 806129776947567877L;
    private String text;
    private int number;
    public MyObject() {
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
}

Wsgen call: (Assuming that the current folder contains the classes (folder org and subdirs with .class files) and an folder out)

wsgen.exe -wsdl -d out -cp . org.wstest.WsTest

This will create several files, including a wsdl. I don't have the infrastructure to test the generated wsdl though.

You could then write a batch script to generate the wsdls for all your classes.

Upvotes: 1

Related Questions