Spring Integration CXF release WebService
1. Import jar package
Because the official download package there are other versions of sprring package, the whole import will produce a version conflict, so get rid of part of the spring, and then create a directory CXF_lib in the project root directory, saving jar package.
2. Write PianoInterface Interface
Create a new PianoInterface method defined interfaces, and add annotations @WebService
package com.CXF;
import javax.jws.WebService;
@WebService
public interface PianoInterface {
//根据Brand查询价格
public int getPriceByBrand(String brand);
}
3. Create PianoService achieve PianoInterface Interface
package com.CXF;
import com.service.PianoServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import javax.jws.WebService;
@WebService(endpointInterface = "com.CXF.PianoInterface")
public class PianoService implements PianoInterface {
/**
* @description 根据品牌查询价格
* @param brand 品牌
* @return int price 价格
* @date 2020/4/2
* @author Charlotte
*/
@Override
public int getPriceByBrand(String brand) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
PianoServiceImpl pianoService = (PianoServiceImpl) ctx.getBean("pianoServiceImpl");
return pianoService.getPriceByBrand(brand);
}
}
4. Modify the spring configuration files
ApplicationContext.xml modify the file
to add
xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
Add to
<!-- 配置发布webservice服务 -->
<jaxws:server address="/PianoWs"
serviceClass="com.CXF.PianoService">
<jaxws:serviceBean>
<bean class="com.CXF.PianoService"></bean>
</jaxws:serviceBean>
</jaxws:server>
Configuring web.xml
<!-- CXF配置Servlet-->
<servlet>
<servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 初始化CXFServlet -->
<!-- <init-param>-->
<!-- <param-name>config-location</param-name>-->
<!-- <param-value>classpath:applicationContext.xml</param-value>-->
<!-- </init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
Published WebService
package com.ui;
import com.CXF.PianoInterface;
import com.CXF.PianoService;
import com.webService.PianoWebService;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import javax.xml.ws.Endpoint;
public class test {
public static void main(String[] args) {
//JAVA自带的WebService发布类
// PianoWebService pianoWebService = new PianoWebService();
// Endpoint. publish("http://localhost:8080/pianowebservice",pianoWebService);
// System.out.println("启动webservice");
//使用CXF发布WebService
JaxWsServerFactoryBean jsfb = new JaxWsServerFactoryBean();
//1.服务提供者实现的接口
jsfb.setServiceClass(PianoInterface.class);
//2.指定访问路径
jsfb.setAddress("http://localhost:8080/ws");
//3.指定服务实现类
jsfb.setServiceBean(new PianoService());
//jsfb.getInInterceptors().add(new LoggingInInterceptor());
//jsfb.getOutInterceptors().add(new LoggingOutInterceptor());
//4.发布
jsfb.create();
System.out.println("发布成功...");
}
}
发布完成之后可以访问http://localhost:8080/ws?wsdl
The client calls WebService
1. Obtain the java file
cmd to enter the JDK bin directory, and then enter the following code
C:\Program Files\Java\jdk1.8.0_73\bin>wsimport -d F:\ -s F:\ -p com http://localhost:8080/pianowebservice?wsdl
Get the file generated in the above-specified file, then copy the file to the project java
2. Import CXF client of all the jar packages, if the conflict can not be imported spring relevant jar package
Written test class
import client.PianoInterface;
import client.PianoInterfaceService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class test {
public static void main(String[] args) {
//JAVA原生
// PianoInterfaceService pianoWebService = new PianoInterfaceService();
// PianoInterface pianoService = pianoWebService.getPianoInterfacePort();
// int price = pianoService.getPriceByBrand("IQOO");
// System.out.println("获得价格:"+price);
//CXF
JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
soapFactoryBean.setAddress("http://localhost:8080/ws");
soapFactoryBean.setServiceClass(PianoInterface.class);
Object o = soapFactoryBean.create();
PianoInterface service = (PianoInterface) o;
int price = service.getPriceByBrand("IQOO");
System.out.println("获得价格:"+price);
}
}
If the message
两个类具有相同的 XML 类型名称 "{http://webService.com/}getPriceResponse"。请使用 @XmlType.name 和 @XmlType.namespace 为类分配不同的名称
This error can add a note in two class,namespace = "http://namespace.thats.not.the.same.as.the.generated"