SOAP web services in Java have traditionally been far more trouble than they’ve been worth. The old model of generating stubs and tweaking configuration files was sufficiently painful that it made the whole process very unpleasant.
In Java EE 5, most of this pain has disappeared; at least for simple web services. JSR-181 defines annotations for web services that remove most of the complexity. For example…
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class SimpleWebService {
@WebMethod
public String doIt(String message) {
return "got "+message;
}
}
In this sample, when the WAR (or EAR) file is deployed to the server, the container will scan for the @WebService annotation and will automatically deploy this as a web service. I use glassfish v2 for this and it works seamlessly.
Oh, there’s still one gotcha of course. The container will only scan for the annotations if it thinks this is a Java EE 5 application. To make sure that it knows that, you have to ensure that your web.xml has the appropriate version attribute on it. The one I’m using for the sample above is this…
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
So what if you’re using maven to do your builds like I do? A quick google search will turn up lots of advice on how you need to install the jaxws-maven-plugin and add it to your build process so that it generates all kinds of pieces. This is great if you’re using older web services but is completely unnecessary in Java EE 5. The only thing you need to do with maven is add dependencies for the Java EE 5 api’s
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.1-1</version>
<scope>provided</scope>
</dependency>
You want the scope to be “provided” so that this jar doesn’t get packaged into your war file. And that’s it.
0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.