springboot2.1.0、webservice简单使用

一)在pom.xml文件中引入webservice需要的jar

<!-- spring webservice -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
        
<!-- CXF webservice -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.7</version>
</dependency>

 

二)、创建一个webservice接口类和实现类

package com.oysept.springboot.webservices;

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

/**
 * 浏览器直接输入访问地址: http://localhost:8080/webservices/WebServices?wsdl
 * @author ouyangjun
 */
@WebService(name = "WebServices", // 暴露服务名称
	targetNamespace = "http://webservices.springboot.oysept.com/" // 命名空间,一般是接口的包名倒序
)
public interface WebServices {
	
    @WebMethod
    @WebResult(name = "helloWebServices", targetNamespace = "")
    public String helloWebServices(@WebParam(name = "param") String param);
}
package com.oysept.springboot.webservices.impl;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.oysept.springboot.webservices.WebServices;

@WebService(serviceName = "WebServices", // 与接口中指定的name一致
	targetNamespace = "http://webservices.springboot.oysept.com/", // 与接口中的命名空间一致,一般是接口的包名倒序
	endpointInterface = "com.oysept.springboot.webservices.WebServices"// 接口地址
)
@Component
public class WebServicesImpl implements WebServices {

    @Override
    public String helloWebServices(String param) {
        return "Hello This is " + param + "!";
    }
}

 

三、由于用到了cxf,新增一个cxf配置类

package com.oysept.springboot.config;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.oysept.springboot.webservices.WebServices;

@Configuration
public class WebServicesConfig {

    @Autowired
    private Bus bus;
	
    @Autowired
    private WebServices webServices;
	
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, webServices);
        endpoint.publish("/WebServices");
        return endpoint;
    }
}

 

四)、在application.properties文件中添加一个cxf配置

server.port=8080

cxf.path=/webservices

 

五)、添加一个webservice测试了,该文章是用代码方式调用webservice,还可以使用常用的一些测试工具。

package com.oysept.springboot.test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.oysept.springboot.webservices.WebServices;

public class WebServicesTest {

    public static void main(String[] args) {
		
        // 接口地址
        String address = "http://localhost:8080/webservices/WebServices?wsdl";
        // 代理工厂
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        // 设置代理地址
        jaxWsProxyFactoryBean.setAddress(address);
        // 设置接口类型
        jaxWsProxyFactoryBean.setServiceClass(WebServices.class);
        // 创建一个代理接口实现
        WebServices cs = (WebServices) jaxWsProxyFactoryBean.create();
		
        // 调用webservices
        String result = cs.helloWebServices("WebServices");
		
        // 打印
        System.out.println(result);
    }
}

 

六)、项目结构图

springboot启动类:

package com.oysept.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * springboot启动类
 * @author ouyangjun
 */
@SpringBootApplication
public class WebservicesApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebservicesApplication.class, args);
    }
}

最后启动WebservicesApplication类,然后再启动webservice测试类,控制台会打印信息。

 

识别二维码关注个人微信公众号


  本章完结,待续,欢迎转载!
 
本文说明:该文章属于原创,如需转载,请标明文章转载来源!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值