使用cxf实现webservice的客户端推送数据到服务端并返回状态给客户端

使用cxf来制作一个简单的客户端推送数据到服务端并返回的一个实例

Server:
1. 创建一个监听器来实现ServletContextListener

package com.webservice;

import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import javax.xml.ws.Endpoint;

@WebListener public class WebServicePublishListener implements ServletContextListener {

    @Override   public void contextDestroyed(ServletContextEvent arg0) {        // TODO Auto-generated method stub

    }

    @Override   public void contextInitialized(ServletContextEvent arg0) {      String address = "http://192.168.11.16:9999/WS_Server/Webservice";      Endpoint.publish(address, new WebServiceImpl());        System.out.println("发布成功");     }

} 
  1. 创建一个接口定义方法
    类上加@javax.jws.WebService
    方法上加@WebMethod
package com.webservice;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.jws.WebMethod;

@javax.jws.WebService
public interface WebService {
    @WebMethod
    public String register(String name);
    @WebMethod
    public String saveCascadeInfo(List list);


}

3.创建一个实现类实现接口
可以传递一个list 也可以传输string

package com.webservice;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.JsonObject;

import net.sf.json.JSONObject;

@javax.jws.WebService
public class WebServiceImpl implements WebService {
    Packages packages = new Packages();

    @Override
    public String register(String jsonStr) {
        System.out.println("服务端接受成功了"+jsonStr);
        return "返回给客户端";
    }

    @Override
    public String saveCascadeInfo(List list) {

        System.out.println(list);
        return "返回结果";
    }
}

然后就可以打开浏览器访问监听器里的发布的url了(http://192.168.11.16:9999/WS_Server/Webservice?wsdl)加上?wsdl来查看里面的内容是否发布成功

Client:
到项目src目录下打开黑窗口(有的是蓝色的)
输入:wsimport -s . http://192.168.11.16:9999/WS_Server/Webservice?wsdl 然后回车(*-s 和 . 的前后都有空格)

然后刷新项目 就会发现项目里面已经多了一个之前服务端一样的包(包里有服务端发布的类和一些其他的类)
配置一个Application-cxf.xml配置文件放到src目录下或者指定目录

<?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">

    <jaxws:client id="webServiceImpl" serviceClass="com.webservice.WebServiceImpl" address="http://192.168.11.16:9999/WS_Server/Webservice?wsdl">

    </jaxws:client>

</beans>

然后配置web.xml因为服务端采用的是注解的方式 所以不用配而客户端采用xml配置文件方式所以得配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <display-name>bos_management_web</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-cxf.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>webservice</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>webservice</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
</web-app>

创建一个测试类

package ns.cxfclient.consumer;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.webservice.WebServiceImpl;


public class CXFClientTest {

    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-cxf.xml");
        WebServiceImpl weatherService = (WebServiceImpl) app.getBean("webServiceImpl");
        String infoByCityName = weatherService.saveCascadeInfo("传输数据到服务端");
        System.out.println(infoByCityName);
    }

}

这样就完成了 cxf简单的客户端推数据到服务端,服务端接受数据并返回给客户端状态;

个人笔记,欢迎指导;

猜你喜欢

转载自blog.csdn.net/Strugglein/article/details/79040106