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

  • 相关阅读:
    转载 logback的使用和logback.xml详解 http://www.cnblogs.com/warking/p/5710303.html
    HTTP 416
    maven 下载jar失败: Missing artifact javax.jms:jms:jar:1.1
    maven 下载jar失败: resolution will not be reattempted until the update interval of central has elapsed or updates are forced
    Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
    转载 Servlet3 的 @WebServlet http://www.cnblogs.com/luxh/archive/2012/06/06/2537458.html
    mysqld服务启动失败, Failed to restart mysqld.service: Unit not found.
    <转载> 22种代码味道(Martin Fowler与Kent Beck) http://blog.csdn.net/lovelion/article/details/9301691
    再谈java clone 以及 浅/深拷贝
    设计模式——再谈工厂模式
  • 原文地址:https://www.cnblogs.com/youqc/p/15157879.html
Copyright © 2020-2023  润新知