编程语言
首页 > 编程语言> > java 写一个webservice接口(部署到Tomcat下)

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.调用接口

标签:java,webservice,Tomcat,apache,cxf,org,services,import,com
来源: https://www.cnblogs.com/youqc/p/15157879.html