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("南京");
}   

猜你喜欢

转载自blog.csdn.net/weixin_38565317/article/details/106968998