java 写一个webservice接口(部署到Tomcat下)

java 写一个webservice接口(部署到Tomcat下)

  1. 创建一个web项目(我的是一个maven项目)
  2. 添加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>
  1. 添加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>
  1. 写一个接口
import javax.jws.WebService;
@WebService
public interface MsgService {
	public String getValue(String name);
}
  1. 写接口的实现类
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;
	}
}

  1. 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>

  1. 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.调用接口
博客地址:https://blog.csdn.net/qq_40002311/article/details/82979720

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值