使用 Apache cxf 创建 WebService 服务端
ws-server-demo
本地服务地址(具体端口跟tomcat配置有关)
http://localhost:8280/services/HelloWorld?wsdl
spring + cfx 配置
maven
添加依赖包
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.2.3</version> </dependency>
web.xml
在web.xml 增加 spring 的配置和 CXFServlet 的配置
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.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>
编写服务
package com.example.demo.ws.service; import javax.jws.WebService; import java.util.Date; @WebService public interface HelloWorld { String sayHi(String text); Date currentDate(); }
package com.example.demo.ws.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.jws.WebService; import java.util.Date; @WebService(endpointInterface = "com.example.demo.ws.service.HelloWorld") public class HelloWorldImpl implements HelloWorld { private Logger logger = LoggerFactory.getLogger(getClass()); public String sayHi(String text) { logger.info("sayHi called"); return "Hello " + text; } @Override public Date currentDate() { logger.info("currentDate called"); return new Date(); } }
applicationContext.xml
在 applicationContext.xml 增加 Service 的配置,记得要 import cxf 的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 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"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:endpoint id="helloWorld" implementor="com.example.demo.ws.service.HelloWorldImpl" address="/HelloWorld"/> </beans>
WSDL
http://localhost:8280/services/HelloWorld?wsdl
就能看到 XML 接口说明
可以直接运行的demo
相关推荐
lantingyue 2020-07-30
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03