一、添加依赖包
<dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2.8</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.7</version> </dependency> <dependency> <groupId>com.sun.xml.stream.buffer</groupId> <artifactId>streambuffer</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.7</version> </dependency> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>policy</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.glassfish.gmbal</groupId> <artifactId>gmbal-api-only</artifactId> <version>3.2.0-b003</version> </dependency> <dependency> <groupId>org.glassfish.ha</groupId> <artifactId>ha-api</artifactId> <version>3.1.9</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.1</version> </dependency>
二、在启动入口中配置servlet,启动web服务并添加servlet相关配置
/** * 启动入口,运行此 main 方法可以启动项目,此 main 方法可以放置在任意的 Class 类定义中,不一定要放于此 */ public static void main(String[] args) { //启动web服务 UndertowServer.create(DdyApplication.class) .configWeb(builder - > { // 配置 Servlet builder.addServlet("testWebservice", "com.sun.xml.ws.transport.http.servlet.WSServlet"); builder.addServletMapping("testWebservice", "/services/*"); builder.setServletLoadOnStartup("testWebservice", 1); //配置启动加载 // 配置 Listener builder.addListener("com.sun.xml.ws.transport.http.servlet.WSServletContextListener"); }).start(); }
三、编写Webservice类
package com.ddy.webservice; import javax.jws.WebParam; import javax.jws.WebService; @WebService public class TestWebservice { public String sendMsg(@WebParam(name = "Name") String name) { System.out.println("name:==============="+name); return "send msg to " + name ; } }
四、配置sun-jaxws.xml
在WEB-INF目录下添加 sun-jaxws.xml文件,配置endpoint,可以配置多个
<?xml version="1.0" encoding="UTF-8"?> <endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"> <endpoint implementation="com.ddy.webservice.TestWebservice" name="wsTest" url-pattern="/services/TestWebservice"/> </endpoints>
五、配置启动类,跳过Webservice相关路由
public void configHandler(Handlers me) { me.add(new UrlSkipHandler(".*/services.*",false)); }
六、启动项目
控制台打印以下日志,说明servlet添加成功
九月 30, 2022 5:34:19 下午 com.sun.xml.ws.transport.http.servlet.WSServletContextListener contextInitialized 信息: WSSERVLET12: JAX-WS 上下文监听程序正在初始化 九月 30, 2022 5:34:21 下午 com.sun.xml.ws.transport.http.servlet.WSServletDelegate <init> 信息: WSSERVLET14: JAX-WS servlet 正在初始化
七、访问
地址栏输入:http://localhost:18080/services/TestWebservice?wsdl 能正常打开wsdl,Webservice发布成功!