October 23, 2009

JAX WS Example

« Java - Testing - Mocked SMTP Server | Main | CSV File Handling in Java »

JAX-WS (Java API for XML - Web Services) represents a Java API for implementing of webservices. The following example describes a basic usage based on Apache CXF and the Spring Framework.

First you implement the interface and the implementation class with JAX WS annotations:

@WebService(targetNamespace = "http://org.developers.blog.examples.jaxws")
//@XmlSeeAlso({TShirt.class, ...}) - map to WSDL Product.class derived subclasses
public interface IOrderService {
    @WebMethod
    void orderProducts(Long customerId, List products) throws OrderException; 
}

@WebService (targetNamespace = "http://org.developers.blog.examples.jaxws",
   endpointInterface =
              "org.developers.blog.examples.jaxws.IOrderService")
public class OrderService implements IOrderService {
    void orderProducts(Long customerId, List products) throws OrderException; {
      //do ordering
    }
}
Instrumentalisation code:
1. WEB-INF/webservices.xml:
<bean id="orderService" class="org.developers.blog.examples.jaxws.OrderService"/>
<jaxws:endpoint id="orderEndpoint"
  implementor="#orderService"
  address="/OrderService"/>
web.xml configuration:
2. WEB-INF/web.xml:
<context-param>
    <param-name>contextConfigLocation</param-name>        
    <param-value>
	WEB-INF/webservices-ctx.xml
    </param-value>
</context-param>
...
<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener> 
...
<servlet>
  <servlet-name>CXFServlet</servlet-name>
  <display-name>CXF Servlet</display-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>CXFServlet</servlet-name>
  <url-pattern>/wsservices/*</url-pattern>
</servlet-mapping>    
Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 10:57 AM in SOA

 

[Trackback URL for this entry]

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« October »
SunMonTueWedThuFriSat
    123
45678910
11121314151617
18192021222324
25262728293031