SpringUG SpringWS Jacobs v3
SpringUG SpringWS Jacobs v3
About Me
John Jacobs
Architect with Credera 10+ years of development experience Mostly in the Java/web space
EJB2 Spring Seam/EJB3
NOT a professional speaker Software World View: Quality, Simplicity Dad, skier, golfer, all-around nice guy! http://twitter.com/john99jacobs
-2-
About Credera
Full-service business and technology consulting firm Professional Services
Technology Solutions (Java, Microsoft, iPhone/mobile, much more) Management Consulting Business Intelligence
Our People
They Rock!!!
-3-
Agenda
Introduction Contract-first v. code-first web services JAX-WS overview Apache CXF Spring Web Services Other frameworks Spring integration Enough alreadyshow me some code!!!! Questions and Answers
-4-
However
-6-
-7-
public class FooServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // return whatever data is being requested PrintWriter out = resp.getWriter(); out.println("someResponse"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // do something with the data that got posted PrintWriter out = resp.getWriter(); out.println("someOtherResponse"); } }
More REST
REST and SOAP both have their places Brand new service? Lots of CRUD operations? Trendy?
REST sounds like a great choice
Need a rigid contract? Want built in validation and to take advantage of strong typing?
SOAP may be just the thing for you
-9-
- 11 -
Contract First
Write the XSD schemas and WSDL contract first Generate or hand-code the Service Endpoint Interface (SEI) and implementation class Its all about the XML! Java is just an implementation detail The folks at SpringSource are big fans JAX-WS can do it too Contract-first is the only option if you choose Spring WS
- 12 -
Loose coupling between WS API and implementation Changes to the contract are tightly controlled
Mismatch will result in an Exception Can be easily validated with a unit test
Code First
Write the Java SEI and implementation class first Configure as a Web Service
Typically using a set of annotations and/or XML Specify the endpoint
- 14 -
- 15 -
Possible strategy: prototype/design with code-first, get a basic WSDL, then cut over to contract-first
- 16 -
Apache CXF
- 17 -
CXF Overview
A combination of two open source projects: Celtix (IONA) and XFire (Codehaus) Implements JAX-WS, among other things Has native APIs that can be used if JAX-WS compliance is not required or to access advanced features
Clear separation of programming interface (JAX-WS) from underlying implementation (think Hibernate and JPA)
- 19 -
PerJAX-WS, SEI is not required, but its a good practice @WebService tells CXF that this is an SEI @WebParam names the parameter in the generated WSDL
- 20 -
serviceName name of the wsdl:service element portName name of the wsdl:port element
- 21 -
- 22 -
Note Springs classpath protocol These resources come from the CXF JAR file This along with the servlet and servlet mapping bootstraps CXF in the Spring container
- 23 -
Hash notation on the implementor attribute creates a reference to a bean Implementor attribute can also refer to a class
<jaxws:endpoint id="userServiceEndpoint" implementor=" com.credera.example.UserServiceImpl" address="/UserService" />
- 24 -
Copy the generated classes into your project Implement business logic in the endpoint implementation class
- 26 -
. . . . (truncated)
- 27 -
Package names are generated based on the target namespace and schema namespace defined in your WSDL
- 28 -
- 29 -
WS-Security is Application layer, use SSL for transport layer security Uses Apache WSS4J under the covers Configured using WSS4JInInterceptor on the endpoint bean WS-Security policies are implemented using a set of callback handlers
- 31 -
Turn on different security policies and options by setting properties in this map per WSS4J documentation
- 34 -
CXF Summary
Strengths
Contract First and Code First First class support for Spring JAX-WS implementation Support for WS-Security Minimal configuration to run in the Spring container Java-centric, no XML programming required except to access advanced features Can incorporate other Spring features like AOP
Weaknesses
Contract First method requires code generation WSDL generated using Code First may not be the most efficient or readable WSS4J is only WS-Security implementation available
- 35 -
- 36 -
Process the incoming XML using your choice of handlers (DOM, SAX, JDOM, DOM4J) Also supports several marshalling technologies (JAXB, Castor, XMLBeans, others)
- 37 -
- 38 -
- 39 -
Xs:elements will be converted into messages and WSDL operations convention over configuration
// Use constructor to read the request XML public UserEndpoint() throws JDOMException { Namespace namespace = Namespace.getNamespace("user", "http://www.credera.com/user/schemas"); employeeIdExpression = XPath.newInstance("//user:employeeId"); employeeIdExpression.addNamespace(namespace); }
- 41 -
- 44 -
XwsSecurityInterceptor
Based on Sun XML and Web Services Security package (XWSS) Requires JDK 1.4+
Wss4jSecurityInterceptor
Based on Apache WSS4J
- 46 -
Use KeyTool to generate a keystore or use the one provided by your certificate authority Write callback handlers We will look at an example of authentication using Spring Security during the demo section
- 48 -
Other Goodies
PayloadValidatingInterceptor validates that request and/or response XMLs conform to the schema definition
<bean id="validatingInterceptor" class="org.springframework.ws.soap.server.endpoint.interceptor.Payl oadValidatingInterceptor"> <property name="schema" value="/WEB-INF/user.xsd"/> <property name="validateRequest" value="false"/> <property name="validateResponse" value="true"/> </bean>
- 49 -
- 50 -
- 51 -
Weaknesses
Only does Contract First Requires programmatic processing of XML message payloads No support for JAX-WS
Other Frameworks
- 53 -
Apache Axis2
Strengths
Next generation of the very popular Axis product Very comprehensive Well documented Has a Spring integration package Services can be deployed at runtime Has REST support
Weaknesses
A little heavyweight, especially in the deployment model Must deploy the Axis2 WAR to your Servlet container Web services are packaged into AAR files (Axis Archive) Positioned as a broader SOA solution
- 54 -
Weaknesses
Documentation Spring integration sub-project is lacking features found in CXF and Spring WS Spring integration may not be production ready
- 55 -
Weaknesses
No direct Spring integration
Tight EJB3 integration Part of the JBoss Application Server Has a native implementation, can also use Metro and CXF
- 56 -
Code
- 57 -
http://sourceforge.net/projects/broadleaf http://twitter.com/broadleaf
- 58 -
- 59 -
Questions
- 60 -