Let’s say you have a Seam application, and you need to create some web services that tie in to it, or you are building a web service and you would like to take advantage of the Seam component model, lifecycle management, etc…
How do you expose Seam functionality through web services? Let’s dive into it:
First off, I’ve started with JBoss 4.0.5, JBoss Web Services 1.2.0, and Seam 1.2.0 (although 1.2.1 should work fine as well).
The first step is to create a class which will actually expose the web services. This class is NOT a Seam component. Create a method for each web service call you want exposed. For instance: getValueA(), getValueB, calculateSomething(int pNumberOne, int pNumberTwo)
Build out the methods, use the Seam Component class (org.jboss.seam.Component) to get access to your Seam components which hold the values or functionality you are trying to expose. This might look something like this:
public int calculateSomething(int pNumberOne, int pNumberTwo) {
NumberCalculator calculator = (NumberCalculator) Component.getInstance("numberCalculator", true);
int result = calculator.calculateCost(pNumberOne, pNumberTwo);
return result;
}
Where the NumberCalculator is your Seam component, named numberCalculator, which is actually going provide the logic.
Next, we need to annotate this class to let JBoss Web Services know you want to expose these methods as a web service. First, annotate the class with:
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
Where WebService is javax.jws.WebService, and SOAPBinding is javax.jws.soap.SOAPBinding. (If you have Eclipse, or a similar IDE, and JBoss WebServices in your project classpath, it will auto import the correct classes for the annotations.)
Next, annotate each method you want exposed with this:
@WebMethod
Where WebService is javax.jws.WebMethod.
The next step will be to setup the web.xml. There are two parts to this. First, we have to define the location of the web service, like this:
CalculatorService
com.digitalsanctuary.calculator.CalculatorWS
CalculatorService
/calculatorService
Then we need to ensure that when this class gets called, the Seam context is there for us, to allow Component to access your Seam components. We do that like this:
Seam Servlet Filter
org.jboss.seam.servlet.SeamServletFilter
Seam Servlet Filter
/calculatorService
Once you deploy your application, the web service should show up here:
http://localhost:8080/jbossws/services
This will also provide links to the wsdl (which you can use to generate your clients).
Leave a Reply