Easiest way to publish Java Web Services --how to

In a previous post I wrote about publishing a Web Service through an embeddable ejb3 implementation, which is good since @Stateless beans can have injected managed entityManagers, in some cases that is the way to go if you don't want to deploy your app in a full J2EE server or you need a more flexible environment.

Just days ago I found that the J2SE itself has an lightweight http server embedded into, and the API itself has an Endpoint class that allows you to publish a Web Service in a J2SE environment.

There is more than one way for doing this, but this is the more automatic I could found, the next is a working example. The JRE version I am using is 6 update 7, I am not sure since when it supports it

-----

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.Endpoint;


@WebService
@SOAPBinding(style=Style.RPC) // without this it does not work automatically.

public class ASimpleTest {
public static class WebServiceTest {
public String callMe(String name) {
return "Hello "+name+" !";  
}
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/sayHello", 
new WebServiceTest());
// http://localhost:8080/sayHello?wsdl will be the 
// place for the wsdl generated on the fly

// remember to use the DNS address of your IP or 
// your IP itself if you want to expose so as to 
// be accessible not only from localhost or
// 127.0.0.1
}
}

----- 


Now you wonder how does this work ? Well, as you publish(...) it starts its embedded http server and binds the webservice automatically generated from the WebServiceTest class to the ip:port you've specified. The threading configuration should be quite ok for most cases.

Have fun trying it !

4 comments:

Anonymous said...

Hi,
For your question it is supported at Java 6 from the beginning, there are more details on the Java6 Web-Services support on my blog: here and here.

There is a support for customization of the published services and also a client stub generation tool.

Anonymous said...

Hi
I hv compiled(in eclipse) successfully but not able to see the wsdl file using http://localhost:8082/sayHello?wsdl (as 8080 busy).Inplace of localhost i tried the ip,dns but not working.

Plase suggest me how to see the wsdl file.

Thans
Amii

Unknown said...

@Amii,

Hi, can you tell me which version of Java SE are you using ?

Also if you leave some contact information it will be easier to help.

Anonymous said...

Dear David

Thanks for ur response .I used jdk6
for this.Pls help me to get the wsdl file.

reachme at deodolphin@gmail.com

Thanks
Amii