【WebService】笔记

工作中需要用到WebService,因此记录下。

环境:JDK1.8、idea、Spring Boot 2.0.6

pom.xml
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!--WebService CXF依赖 start-->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.1.12</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.1.12</version>
		</dependency>
		<!--WebService CXF依赖 end-->

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
		</dependency>
	</dependencies>
application.yml
server:
  port: 8001
接口
import org.springframework.stereotype.Service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.List;

/**
 * @author zhaoYang
 * @Title: web-service-study
 * @Package com.zhao.service
 * @date 2018/11/13 21:43
 */
@SuppressWarnings("restriction")
@WebService(name = "services", // 暴露服务名称
        targetNamespace = "http://service.zhao.com/"// 命名空间,一般是接口的包名倒序
)
@Service
public interface IWebService {

    @WebMethod
    String getUserName(@WebParam(name = "userName")String userName);

    @WebMethod
    List<String> getList(@WebParam(name = "list")List<String> list);

}
实现类
import com.zhao.service.IWebService;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import java.util.List;

/**
 * @author zhaoYang
 * @Title: web-service-study
 * @Package com.zhao.service.impl
 * @date 2018/11/13 21:47
 */
@WebService(serviceName = "services", // 与接口中指定的name一致
        portName = "WebServiceImpl",
        targetNamespace = "http://service.zhao.com/", // 与接口中的命名空间一致,一般是接口的包名倒序
        endpointInterface = "com.zhao.service.IWebService") // 接口地址
@Component
public class WebServiceImpl implements IWebService{

    @Override
    public String getUserName(String userName) {

        return "This is Web Service and my name is "+userName;
    }

    @Override
    public List<String> getList(List<String> list) {
        list.add("list_1");
        list.add("list_2");
        list.add("list_3");
        list.add("list_4");
        list.add("list_5");
        return list;
    }
}
config类
import com.zhao.service.impl.WebServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import javax.xml.ws.Endpoint;

/**
 * http://IP/域名:项目端口号/第41行设置的mapping/第51行设置的endpoint?wsdl
 * 访问demo:http://localhost:8001/myWs/services?wsdl
 * @author zhaoYang
 * @Title: web-service-study
 * @Package com.zhao.config
 * @date 2018/11/13 21:50
 */
@Configuration
public class WebServiceCxfConfig {

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public WebServiceImpl webServiceImpl() {
        return new WebServiceImpl();
    }

    /**
     * 自定义servlet的访问
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/myWs/*");
        bean.setLoadOnStartup(0);
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;
    }

    /** JAX-WS **/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), webServiceImpl());
        endpoint.publish("/services");
        return endpoint;
    }

}
启动项目,对外发布WebService接口

访问WebService有两种方式,如下⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇

测试类:第一种
import com.zhao.client.Services_Service;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.jupiter.api.Test;


/**
 * @author zhaoYang
 * @Title: web-service-study
 * @Package com.zhao.main
 * @date 2018/11/13 21:56
 */
public class Main {

    @Test
    public void test_1(){
        String userName="zhaoYoung";
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8001/myWs/services?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("getUserName", userName);
            System.out.println("\r\n返回数据:\r\n" + objects[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
执行结果

在这里插入图片描述

测试类:第二种
需要通过命令生成WebService Client代码

wsimport http://localhost:8001/myWs/services?wsdl -keep -p com.zhao.client -s d:/ws -encoding utf-8

说明:wsimport webservice接口 -keep -p 包名 -s 生成源代码的位置 -encoding utf-8

注意!!!需要提前创建生成源代码的位置

cmd中执行以上命令,若出现wsimport不是内部或者外部命令的解决方法,则是本地JDK环境变量配置有问题

执行成功后的样子如下:
在这里插入图片描述

在这里插入图片描述

接下来就是如何连接WebService
import com.zhao.client.Services_Service;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.jupiter.api.Test;

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


/**
 * @author zhaoYang
 * @Title: web-service-study
 * @Package com.zhao.main
 * @date 2018/11/13 21:56
 */
public class Main {
    @Test
    public void test_2(){
    	// 若找不到以下类,则全局搜索 @WebServiceClient 所在的类就是下面的这个
        Services_Service service=new Services_Service();
        List<String> list=new ArrayList<>();
        list.add("Web Service");
        List<String> stringList = service.getWebServiceImpl().getList(list);
        System.out.println("\r\n返回数据:\r\n" +stringList);

    }

}
执行结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值