Spring Boot integration CXF release and call WebService

Source: Internet
Author: User
Tags soap wsdl

I. Preface
Speaking of the Web service in recent years, the restful way, a large replacement of the traditional SOAP web service trend, but some unique or relatively old systems still use traditional SOAP Web service, such as banks, airlines ticket query interface. This blog is mainly about spring boot integration CXF release WebService service and spring boot Integration CXF client invoke WebService service
This case uses the MAVEN approach
Two. Encoding
List of core documents
1.pom.xml

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "><modelVersion>4.0.0</modelVersion><groupId> Com.leftso</groupid><artifactid>demo-webservice-cxf</artifactid><version>0.0.1- snapshot</version><packaging>jar</packaging><name>demo-webservice-cxf</name>< Description>demo Project for Spring Boot security</description><parent><groupid> org.springframework.boot</groupid><artifactid>spring-boot-starter-parent</artifactid>< Version>1.4.5.release</version><relativepath/> <!--lookup parent from repository--></ parent><properties><project.build.sourceencoding>utf-8</project.build.sourceencoding>< Project.reporting.outputencoding>utf-8</project.reporting.outputencoding><java.version>1.8</java.version></properties>< Dependencies><dependency><groupid>org.springframework.boot</groupid><artifactid> Spring-boot-starter</artifactid></dependency><dependency><groupid> org.springframework.boot</groupid><artifactid>spring-boot-starter-web</artifactid></ dependency><!--CXF WebService--><dependency><groupid>org.apache.cxf</groupid>< artifactid>cxf-spring-boot-starter-jaxws</artifactid><version>3.1.7</version></ dependency><!--CXF WebService--><dependency><groupid>org.springframework.boot</groupid ><artifactid>spring-boot-starter-test</artifactid><scope>test</scope></ Dependency></dependencies><build><plugins><plugin><groupid> Org.springframework.boot</groupid><artifactid>spring-boot-maveN-plugin</artifactid></plugin></plugins></build></project> 

2.commonservice.java Service Interface

Package Com.leftso.webservice;import Javax.jws.webmethod;import Javax.jws.webparam;import javax.jws.WebResult; Import javax.jws.webservice;/*** Interface * * @author leftso**/@WebService (name = "Commonservice",//Expose service name targetnamespace = "H ttp://webservice.leftso.com/"//namespace, typically the package name of the interface is reversed) public interface Commonservice {@WebMethod @webresult (name =" String " , targetnamespace = "") public string SayHello (@WebParam (name = "UserName") string name);} 3. Interface implementation Package Com.leftso.webservice;import Javax.jws.webservice;import org.springframework.stereotype.component;/ Interface Implementation * * @author leftso**/@WebService (serviceName = "Commonservice",//and the name specified in the interface targetnamespace = "HTTP/ webservice.leftso.com/",//is consistent with the namespace in the interface, usually the package name of the interface is inverted endpointinterface =" com.leftso.webservice.CommonService "//interface Address) @ Componentpublic class Commonserviceimp implements Commonservice {@Overridepublic string SayHello (string name) {return] Hello, "+ Name;}} 4. Configure the CXF Service Release, the default service under the host:port/services/*** path package Com.leftso.config;import javax.xml.Ws. Endpoint;import Org.apache.cxf.bus;import Org.apache.cxf.jaxws.endpointimpl;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.annotation.Bean; Import Org.springframework.context.annotation.configuration;import com.leftso.webservice.commonservice;@ Configurationpublic class Cxfconfig {@Autowiredprivate bus bus; @AutowiredCommonService commonservice;/** Jax-ws **/@ Beanpublic Endpoint Endpoint () {Endpointimpl Endpoint = new Endpointimpl (bus, commonservice); Endpoint.publish ("/ Commonservice "); return endpoint;}}

  

This is equivalent to publishing the Commonservice interface under Path/services/commonservice, the WSDL document path is HTTP://LOCALHOST:8080/SERVICES/COMMONSERVICE?WSDL

Create a CXF-based client call WebService interface (Java classes are not generated using WSDL documents)

Package Com.leftso.client;import Org.apache.cxf.endpoint.client;import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean ; Import Org.apache.cxf.jaxws.endpoint.dynamic.jaxwsdynamicclientfactory;import Com.leftso.webservice.commonservice;public class Cxfclient {public static void main (string[] args) {CL1 ();} /*** Mode 1. Proxy class factory way, need to get to each other's interface */public static void Cl1 () {try {//interface address string "=" http://localhost:8080/services/ COMMONSERVICE?WSDL ";//Agent factory Jaxwsproxyfactorybean Jaxwsproxyfactorybean = new Jaxwsproxyfactorybean ();// Set the proxy address jaxwsproxyfactorybean.setaddress;//Set the interface type Jaxwsproxyfactorybean.setserviceclass ( Commonservice.class);//Create a proxy interface implementation Commonservice CS = (commonservice) jaxwsproxyfactorybean.create ();//Data Preparation string UserName = "Leftso";//Invokes the method call of the proxy interface and returns the result string = Cs.sayhello (UserName); SYSTEM.OUT.PRINTLN ("return Result:" + result);} catch (Exception e) {e.printstacktrace ();}} /*** Dynamic call mode */public static void Cl2 () {//Create dynamic client jaxwsdynamicclientfactory DCF = Jaxwsdynamicclientfactory.newinsTance (); Client client = dcf.createclient ("http://localhost:8080/services/CommonService?wsdl"),//password required, username and password Client.getoutinterceptors (). Add (New Clientlogininterceptor (user_name,//Pass_word)); object[] objects = new Object[0] ; try {//Invoke ("method name", parameter 1, parameter 2, parameter 3 ...); O bjects = Client.invoke ("SayHello", "Leftso"); SYSTEM.OUT.PRINTLN ("Return Data:" + objects[0]);} catch (Java.lang.Exception e) {e.printstacktrace ();}}}

After the call returns the result output as
Hello,leftso

Three. Question Q&a
3.1 Known Issues 1
Upgrade the spring boot version of the example above to 1.5.x. Start Project Error:

Java.lang.noclassdeffounderror:org/springframework/boot/context/embedded/servletregistrationbeanat JAVA.LANG.CLASS.GETDECLAREDMETHODS0 (Native Method) ~[na:1.8.0_40]at java.lang.Class.privateGetDeclaredMethods ( class.java:2701) ~[na:1.8.0_40]at java.lang.Class.getDeclaredMethods (class.java:1975) ~[na:1.8.0_40]at Org.springframework.util.ReflectionUtils.getDeclaredMethods (reflectionutils.java:613) ~[ Spring-core-4.3.12.release.jar:4.3.12.release]at Org.springframework.util.ReflectionUtils.doWithMethods ( reflectionutils.java:524) ~[spring-core-4.3.12.release.jar:4.3.12.release]at Org.springframework.util.ReflectionUtils.doWithMethods (reflectionutils.java:510) ~[ Spring-core-4.3.12.release.jar:4.3.12.release]at Org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods (reflectionutils.java:570) ~[ Spring-core-4.3.12.release.jar:4.3.12.release]at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod ( abstractautowirecapablebeanfactory.java:697) ~[spring-beans-4.3.12.release.jar:4.3.12.release]at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType ( abstractautowirecapablebeanfactory.java:640) ~[spring-beans-4.3.12.release.jar:4.3.12.release]at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType ( abstractautowirecapablebeanfactory.java:609) ~[spring-beans-4.3.12.release.jar:4.3.12.release]at Org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean (abstractbeanfactory.java:1484) ~[ Spring-beans-4.3.12.release.jar:4.3.12.release]at Org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType ( defaultlistablebeanfactory.java:425) ~[spring-beans-4.3.12.release.jar:4.3.12.release]at Org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType ( defaultlistablebeanfactory.java:395) ~[spring-beans-4.3.12.release.jar:4.3.12.release]at Org.springframework.context.support.PostProcEssorregistrationdelegate.invokebeanfactorypostprocessors (postprocessorregistrationdelegate.java:96) ~[ Spring-context-4.3.12.release.jar:4.3.12.release]at Org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors ( abstractapplicationcontext.java:687) ~[spring-context-4.3.12.release.jar:4.3.12.release]at Org.springframework.context.support.AbstractApplicationContext.refresh (abstractapplicationcontext.java:525) ~[ Spring-context-4.3.12.release.jar:4.3.12.release]at Org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh ( embeddedwebapplicationcontext.java:122) ~[spring-boot-1.5.8.release.jar:1.5.8.release]at Org.springframework.boot.SpringApplication.refresh (springapplication.java:693) [Spring-boot-1.5.8.release.jar : 1.5.8.release]at Org.springframework.boot.SpringApplication.refreshContext (springapplication.java:360) [ Spring-boot-1.5.8.release.jar:1.5.8.release]at Org.springframework.boot.SpringApplication.run ( Springapplication.java:303) [Spring-boot-1.5.8.release.jar:1.5.8.release]at Org.springframework.boot.SpringApplication.run ( springapplication.java:1118) [Spring-boot-1.5.8.release.jar:1.5.8.release]at Org.springframework.boot.SpringApplication.run (springapplication.java:1107) [Spring-boot-1.5.8.release.jar : 1.5.8.release]at Com.leftso.Application.main (application.java:10) [classes/:na]caused by: Java.lang.ClassNotFoundException:org.springframework.boot.context.embedded.ServletRegistrationBeanat Java.net.URLClassLoader.findClass (urlclassloader.java:381) ~[na:1.8.0_40]at Java.lang.ClassLoader.loadClass ( classloader.java:424) ~[na:1.8.0_40]at Sun.misc.launcher$appclassloader.loadclass (Launcher.java:331) ~[na:1.8.0_ 40]at Java.lang.ClassLoader.loadClass (classloader.java:357) ~[na:1.8.0_40] ... Common frames omitted

Workaround:
The first time this problem occurred, I thought it was the regular MAVEN management jar package dependency problem. There are small partners in the back group trying to prove that it is not Maven's dependency package problem. This is a version incompatibility issue with spring boot and cxf.
Known compatible versions:
Spring boot 1.4.x------>CXF-SPRING-BOOT-STARTER-JAXWS 3.1.x
Spring boot 1.5.x------->cxf-spring-boot-starter-jaxws 3.2.x
So the above problem, and the spring boot 1.5.x with the small partners, please upgrade the CXF-SPRING-BOOT-STARTER-JAXWS version to 3.2.1 can be resolved.

3.2 Modifying the default/services path after spring boot CXF consolidation
Configuring in the Spring Boot application configuration file
Cxf.path=/yourpath

This article transferred from: http://www.leftso.com/blog/144.html

Spring Boot integration CXF release and call WebService

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.