java 写一个webservice接口(部署到Tomcat下)
2021/8/18 17:36:17
本文主要是介绍java 写一个webservice接口(部署到Tomcat下),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
java 写一个webservice接口(部署到Tomcat下)
- 创建一个web项目(我的是一个maven项目)
- 添加jar包
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 这里直接导入整个项目下的所有jar包,其中去除几个 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>apache-cxf</artifactId> <version>3.2.6</version> <type>pom</type> <exclusions> <exclusion> <artifactId>cxf-services-wsn-api</artifactId> <groupId>org.apache.cxf.services.wsn</groupId> </exclusion> <exclusion> <artifactId>cxf-services-wsn-core</artifactId> <groupId>org.apache.cxf.services.wsn</groupId> </exclusion> <exclusion> <artifactId>cxf-services-ws-discovery-api</artifactId> <groupId>org.apache.cxf.services.ws-discovery</groupId> </exclusion> <exclusion> <artifactId>cxf-services-ws-discovery-service</artifactId> <groupId>org.apache.cxf.services.ws-discovery</groupId> </exclusion> </exclusions> </dependency> </dependencies>
- 添加web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
- 写一个接口
import javax.jws.WebService; @WebService public interface MsgService { public String getValue(String name); }
- 写接口的实现类
package com.msg.webservice.impl; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.jws.WebService; import com.msg.util.Log_Exception; import com.msg.util.UrlUtil; import com.msg.util.Util; import com.msg.webservice.MsgService; @WebService(endpointInterface = "com.msg.webservice.MsgService") public class MsgServiceImpl implements MsgService { public String getValue(String name) { return "我叫" + name; } }
- cxf-servlet.xml
<?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" /> //implementor:实现类的地址 address:调用时的地址 <jaxws:endpoint id="personQueryService" implementor="com.msg.webservice.impl.MsgServiceImpl" address="/msgservice" /> </beans>
- client-beans.xml
<?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/schema/jaxws.xsd"> //class:接口地址 <bean id="client" class="com.msg.webservice.MsgService" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> //value:接口地址 <property name="serviceClass" value="com.msg.webservice.MsgService" /> //value:调用时的地址 <property name="address" value="http://192.168.1.10:9090/MSService/services/msgservice" /> </bean> </beans>
8.启动Tomcat
9.在浏览器输入
http://192.168.1.10:9090/MSService/services/msgservice?wsdl
10.调用接口
这篇关于java 写一个webservice接口(部署到Tomcat下)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-27数据结构与算法面试题详解及练习
- 2024-12-27网络请求面试题详解与实战
- 2024-12-27数据结构和算法面试真题详解与实战教程
- 2024-12-27网络请求面试真题解析与实战教程
- 2024-12-27数据结构和算法大厂面试真题详解与实战指南
- 2024-12-27TS大厂面试真题解析与应对策略
- 2024-12-27TS大厂面试真题详解与解析
- 2024-12-27网站安全入门:如何识别和修复漏洞
- 2024-12-27SQL注入基础教程
- 2024-12-27初学者指南:理解和修复跨域漏洞