SSM集成webservice-绝对能用

ssm集成webservice,我这里用的是CXF,因为对于Axis和Afire来说CXF算是非常好的,这里就不对他们的区别多说了,首先:

1、加入相应的依赖:

<!--webServiceCxf  -->
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.2.7</version>
        </dependency>
        <dependency> 
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-simple -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-simple</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-wsdl -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-wsdl</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-bindings-soap -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-bindings-soap</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.ws.xmlschema/xmlschema-core -->
        <dependency>
            <groupId>org.apache.ws.xmlschema</groupId>
            <artifactId>xmlschema-core</artifactId>
            <version>2.2.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-databinding-jaxb -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-databinding-jaxb</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-ws-addr -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-addr</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-ws-policy -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-policy</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.neethi/neethi -->
        <dependency>
            <groupId>org.apache.neethi</groupId>
            <artifactId>neethi</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.codehaus.woodstox/stax2-api -->
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>stax2-api</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.codehaus.woodstox/woodstox-core-asl -->
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>woodstox-core-asl</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
          <groupId>com.qcloud</groupId>
          <artifactId>qcloud-image-sdk</artifactId>
          <version>2.3.3</version>
        </dependency>

2、在web.xml里加上webservice的servlet

<!--注册一个用于接收其他工程向本工程发送的webservice请求的请求接收器-->
      <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <!--配置过滤请求地址项目名+webService+address+?wsdl-->
      <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/webService/*</url-pattern>
      </servlet-mapping>

3、在spring.xml加入相应的配置

         xmlns:jaxws="http://cxf.apache.org/jaxws"
         xmlns:soap="http://cxf.apache.org/bindings/soap"

         http://cxf.apache.org/jaxws
         http://cxf.apache.org/schemas/jaxws.xsd

这是图中的配置,位置最好一样,因为ssm框架太操蛋。

4、开始写接口:

扫描二维码关注公众号,回复: 8684911 查看本文章

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.yltd.cnyun.common.area.entity.Area;
/**
 * 
 * 基于soap的服务
 */
@WebService
public interface TestService {

     // 提供一个对外公开的服务
     @WebMethod(operationName = "sayHellow")
     public String sayHellow(@WebParam(name = "username") String username);

}

实现类:

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import com.yltd.cnyun.common.area.entity.Area;
import com.yltd.cnyun.common.area.service.AreaService;

/**
 * 服务实现类
 */
@WebService    
public class TestServiceImpl implements TestService {

    @Autowired
    AreaService areaService;
    
    
    @WebMethod(operationName = "sayHellow")
    @Override
    public String sayHellow(@WebParam(name = "username")String username) {
        // TODO Auto-generated method stub
        return username+"你好!";
    }
}

5、把写好的接口配置在spring.xml中,项目启动的时候扫描spring.xml自动发布出去

<!-- 配置方式1   注意:serviceClass为接口类并非实现类 -->
    <!-- <jaxws:server serviceClass="com.yltd.cnyun.common.webservice.service.TestService" address="/webService"></jaxws:server> -->
    
    <!-- 配置方式2    注意:implementor为接口的具体实现类 -->
    <jaxws:endpoint implementor="com.yltd.cnyun.common.webservice.service.TestServiceImpl" address="webService" ></jaxws:endpoint>

两个方式都可以

然后启动项目,访问路径localhost:8080/项目名字/webSerive/webService?wsdl出现,

说明你发布成功了。

6、最后一步测试你发布的接口:

@SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
        
        System.out.println("开始调用接口!");
        Properties props = System.getProperties(); 
        props.setProperty("org.apache.cxf.stax.allowInsecureParser", "1"); 
        props.setProperty("UseSunHttpHandler", "true");
        //采用动态工厂方式 不需要指定服务接口
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://10.10.26.108:8080/cnyun-web/webService/webService?wsdl");
        Object[] res = client.invoke("sayHellow", "admin");
        System.out.println("Echo response: sayHellow" + res[0]);

}

}

console不报错有数据,就说明你调通了,这里客户端调服务端的时候,我写的是java程序测试用的,相应的jar包比较多,图片给你贴出来:

大部分的包是没用的,我去官网把apache.cxf所有的包都到进来了,你用的时候自己删除吧,可能会出现包的版本不一样报错的问题,这都是小问题了,自己搞一下,哪看不懂的可以私信。

发布了31 篇原创文章 · 获赞 24 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/hengliang_/article/details/86507388