apt vs wsgen vs wsimport .Confusion on what to use when and why not to use the other

Bhavik Shah picture Bhavik Shah · Apr 6, 2014 · Viewed 7.4k times · Source

I have been trying to write a web service (JAX-WS) and I have gone through a number of tutorials ranging from 2006 to 2013 ones. Most of them are with respect to some IDE. Those which talk about manual creation/deployment of web service are with ant scripts. Till here everything is fine.

The moment I check the ant scripts, the confusion starts. some old tutorials use APT task to compile the SEI and then wsgen is used to generate the artifacts. the newer ones use only wsgen (although APT is defined as taskdef in ant scripts). Also , as I have been reading about the JAVA 7 documentation, it says you dont need to use wsgen as javac itself does all compilations and the artifacts req. for WS is generated at runtime dynamically.

I tried to use javac command on SEI and deployed it on tomcat but it didnt work.

Can anyone please clarify as in what commands I need to use in JAVA SE 7 edition to deploy a web service.

Also, I want to know what does each command generate and when to use which commands.

According to my knowledge wsimport is not needed for development and deployment , but only need to build the WS-client. Is this correct? If not please provide me pointers to clear my understanding.

Thanks in Advance

Also if I am repeat my self then sorry as I am new to StackOverflow and not very familiar with this. :)

Answer

FewDexter picture FewDexter · Apr 6, 2014

To create a Web Service using the Java-WS specification, you need several artifacts. The purpose of "apt", "wsgen" and "wsimport" is to automate this procedure.

There are two ways to create a Web Service: Bottom-Up (First code, then WSDL) and Top-Down (First WSDL then code).

Bottom-Up approach:

  • apt: Uses source code, generates WSDL (and artifacts)
  • wsgen: Uses compiled code, generates WSDL (and artifacts)

Top-Down

  • wsimport: Uses the WSDL, generates java code for the service/client implementation.

The advantage of using apt is that having the source code the script will be able to get the parameters names and use them on the WSDL.

Web Service Deployment

For production you would need a Web Container which can be in charge of security, scalability and resource management, however, for testing purposes you can deploy your web services using the built-in web server on Java SE by doing:

 @WebService 
 public class MyService {
                                          
    public static void main(String args[]) {   
        MyService service = new MyService();   
        String url = "service/";   
        Endpoint ep = new Endpoint(url, service); 
    }
     

    @WebMethod 
    public String getInfo() {   
        return "Service info"; 
    }
}

This piece of code would generate the WSDL and publish the service at your localhost/service.