spring boot集成webservice接口

依赖集成

当前spring boot 版本是 2.0.1.RELEASE, 其对应的cxf依赖版本为:3.2.4, 详情如下:

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
	<version>3.2.4</version>
</dependency>

代码实现

接口实现
package com.spring.login.api;
import javax.jws.WebService;
/**
 *  天气查询
 */
 @WebService
 public interface WeatherService {

    /**
     *  查询温度
     * @param city
     * @return
     */
    String queryTemperature(String city);

    /**
     *  查询降雨量
     * @param city
     * @return
     */
    String queryRainfall(String city);

    /**
     *  查询风力
     * @param city
     * @return
     */
    String queryWindLevel(String city);

}
接口实现类:
package com.spring.login.api.impl;
  import com.spring.login.api.WeatherService;
  import org.springframework.stereotype.Component;
  import javax.jws.WebService;
/**
 *  天气管理
 */
@Component
@WebService
public class WeatherServiceImpl implements WeatherService {

    @Override
    public String queryTemperature(String city) {

        return  city + ": 30摄氏度";
    }

    @Override
    public String queryRainfall(String city) {

        return city + ": 3500毫米";
    }

    @Override
    public String queryWindLevel(String city) {

        return city + ": 5-7级别";
    }

}
实例化配置
package com.spring.login.config;

import com.spring.login.api.WeatherService;
import org.apache.cxf.Bus;
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 javax.annotation.Resource;
import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfg {

    @Resource
    private Bus bus;

    @Resource
    private WeatherService weatherService;

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){

        return new ServletRegistrationBean(new CXFServlet(),"/services/*");
    }

    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(bus,weatherService);
        endpoint.publish("/weatherService");
        return endpoint;
    }

}

访问地址: http://localhost:9090/services/weatherService?wsdl

在这里插入图片描述

客户端接收

方式一:
该方式要求接口的命名空间和接口的目录一致,代码如下:

public String queryService(){
   JaxWsProxyFactoryBean jaxWsProxyFactoryBeanx = new JaxWsProxyFactoryBean();
   jaxWsProxyFactoryBeanx.setAddress(http://127.0.0.1:9090/services/weatherService?wsdl);
   jaxWsProxyFactoryBeanx.setServiceClass(WeatherService.class);
   WeatherService weatherService = (WeatherService) jaxWsProxyFactoryBeanx.create();
   return weatherService .queryTemperature("南京");
}   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值