Tuesday 23 February 2016

create SOAP based Webservice - Spring & CXF

web.xml:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"         id="WebApp_ID" version="2.5">
    <display-name>myws</display-name>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/application-config.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

application-config.xml :

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:jaxws="http://cxf.apache.org/jaxws"       xmlns:jaxrs="http://cxf.apache.org/jaxrs"       xmlns:soap="http://cxf.apache.org/bindings/soap"       xmlns:context="http://www.springframework.org/schema/context"       xmlns="http://www.springframework.org/schema/beans"       xsi:schemaLocation="http://www.springframework.org/schema/beans                            http://www.springframework.org/schema/beans/spring-beans.xsd                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd                            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd                            http://cxf.apache.org/bindings/soap                            http://cxf.apache.org/schemas/configuration/soap.xsd                            http://www.springframework.org/schema/context                            http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.my.webservice"/>

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    <import resource="db-config.xml"/>


    <bean id="myWebService" class="com.my.webservice.v1.service.impl.WebServiceImpl"/>
 <bean id="auditInInterceptor" class="com.my.webservice.interceptors.AuditLogInterceptor"/>
    <bean id="logOut" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
    <bean id="logIn" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>

    <jaxws:endpoint id="WebService" implementor="#myWebService" address="/MyWebService">
        <jaxws:binding>
            <soap:soapBinding mtomEnabled="true" version="1.2"/>
        </jaxws:binding>
        <jaxws:properties>
            <entry key="schema-validation-enabled" value="true"/>
            <entry key="faultStackTraceEnabled" value="true"/>
        </jaxws:properties>
        <jaxws:inInterceptors>
            <ref bean="logIn"/>
            <ref bean="auditInInterceptor"/>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
            <ref bean="logOut"/>
        </jaxws:outInterceptors>
    </jaxws:endpoint>

   <!-- <bean id="xmlSigInHandler" class="org.apache.cxf.rs.security.xml.XmlSigInHandler"/>-->    <bean id="xmlEncInHandler" class="org.apache.cxf.rs.security.xml.XmlEncInHandler">
        <property name="encryptionProperties" ref="encProps"/>
    </bean>

    <bean id="xmlEncOutHandler" class="org.apache.cxf.rs.security.xml.XmlEncOutInterceptor">
        <property name="symmetricEncAlgorithm" value="aes128-cbc"/>
    </bean>

    <bean id="encProps" class="org.apache.cxf.rs.security.xml.EncryptionProperties">
        <property name="encryptionKeyTransportAlgo"                  value="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
        <property name="encryptionSymmetricKeyAlgo"                  value="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
    </bean>

    <jaxrs:server id="myRestService" address="/myRestServices">
        <jaxrs:serviceBeans>
            <ref bean="myWebService"/>
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json"/>
            <entry key="xml" value="application/xml"/>
        </jaxrs:extensionMappings>
        <jaxrs:providers>
            <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider"/>
            <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
            <!--<ref bean="xmlEncInHandler"/>-->        </jaxrs:providers>
        <jaxrs:inInterceptors>
            <ref bean="logIn"/>
            <ref bean="auditInInterceptor"/>
        </jaxrs:inInterceptors>
        <jaxrs:outInterceptors>
           <ref bean="xmlEncOutHandler"/>
        </jaxrs:outInterceptors>
        <jaxrs:properties>
            <entry key="security.callback-handler" value="com.my.webservice.v1.service.impl.KeystorePasswordCallback"/>
            <entry key="security.encryption.username" value="tomcat"/>
            <entry key="security.encryption.properties" value="file:///C:/Users/kuku/tomcat.properties"/>
        </jaxrs:properties>
    </jaxrs:server>
</beans>


import javax.jws.WebService;

@WebService(targetNamespace = "http://webservice.my.com/v1", name = "MyWebServicePortType")
public interface WebServiceInterface {
.
.
.
}

import org.apache.cxf.annotations.SchemaValidation;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.ws.rs.*;
import java.io.IOException;
import java.util.List;

@WebService(name = "MyWebService", targetNamespace = "http://webservice.my.com/v1", serviceName = "MyWebService", endpointInterface = "com.my.webservice.v1.service.MyWebService", portName = "MyWebServicePortName")
@SchemaValidation(type = SchemaValidation.SchemaValidationType.BOTH)
public class WebServiceImpl implements WebServiceInterface {
.
.
.
}


http://localhost:8080/MyWS/services/MyWebService?wsdl

MyWS - Project name (war name)


No comments:

Post a Comment