springboot+cxf框架开发SOAP接口

什么是SOAP

SOAP是 simple object access protocol(简单对象访问协议)的缩写。这个协议是用http的post请求实现的,跟一般的post请求不同的是,在请求的header里添加了一些标志来说明自己是SOAP请求,然后body里传XML数据。

什么是WSDL

web service description language的缩写,想当于一个SOAP接口的说明书。

关于soap和wsdl详细可以参考这篇文章:https://www.cnblogs.com/JeffreySun/archive/2009/12/14/1623766.html

springboot+cxf框架开发SOAP接口

1、添加依赖,springboot版本过高会报错。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> 
</parent>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.5</version>
</dependency>

2、创建服务接口WeatherService

@WebService(targetNamespace = "http://service.webservice2020.ysp.org")
public interface WeatherService {
    @WebMethod
    String queryWeather(@WebParam(name = "city") String city);
}

3、实现类

@Service
@WebService(serviceName = "weatherService" 
        ,name="WeatherService" //porType名称 客户端生成代码时为接口名称
        ,targetNamespace = "http://service.webservice2020.ysp.org"//wsdl命名空间
        ,endpointInterface = "org.ysp.webservice2020.service.WeatherService")
public class WeatherServiceImpl implements WeatherService {

    public final static String[] WEATHER_DESC = new String[]{"晴天", "多云", "暴雨", "大雨", "中雨", "小雨"};

    @Override
    public String queryWeather(String city) {
        StringBuilder builder = new StringBuilder(city);
        builder.append("天气:");
        builder.append(WEATHER_DESC[getRandomNumber(5)]);
        return builder.toString();
    }

    private static int getRandomNumber(int num) {
        return (int) (Math.random() * num);
    }
}

4、配置类

@Configuration
public class AppConfig {
    @Autowired
    private Bus bus;
    @Autowired
    private WeatherService weatherService;


    /**
     * 添加普通的controller处理,兼容rest接口用,非必须
     * @return
     */
    @Bean
    public ServletRegistrationBean dispatcherRestServlet() {
        AnnotationConfigWebApplicationContext context
                = new AnnotationConfigWebApplicationContext();
        //替换成自己的controller包路径
        context.scan("org.ysp.webservice2020.controller");
        DispatcherServlet disp = new DispatcherServlet(context);
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(disp);
        registrationBean.setLoadOnStartup(1);
        //映射路径自定义,必须设置一个不重复的名称
        registrationBean.addUrlMappings("/rest/*");
        registrationBean.setName("rest");
        return registrationBean;
    }

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

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }
}

调用客户端

1、maven依赖和插件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/> 
</parent>

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

<build>
    <plugins>
        <!-- cxf-codegen-plugin -->
        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>3.2.5</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
                        <wsdlOptions>
                            <wsdlOption>
                                <wsdl>src/main/resources/wsdl/weather.wsdl</wsdl>
                                <wsdlLocation>classpath:wsdl/weather.wsdl</wsdlLocation>
                            </wsdlOption>
                        </wsdlOptions>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2、将WSDL文件放到resources下的wsdl文件夹,然后执行mvn generate-sources

在这里插入图片描述

3、配置类

@Configuration
public class AppConfig {

    /**
     *  以接口代理方式进行调用 WeatherService接口
     */
    @Bean("cxfProxy")
    public WeatherService createAuthorPortTypeProxy() {
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setServiceClass(WeatherService.class);
        //服务地址
        jaxWsProxyFactoryBean.setAddress("http://localhost:8080/ws/weatherService");
        return (WeatherService) jaxWsProxyFactoryBean.create();
    }
    
}

4、controller调用

@RestController
@RequestMapping("/cxf")
public class ClientController {
    @Autowired
    @Qualifier("cxfProxy")
    WeatherService weatherService;


    @RequestMapping("/queryWeather")
    @ResponseBody
    public Map<String,Object> queryWeather(String city){

        Map<String,Object> result = new HashMap<>();

        result.put("code",0);
        result.put("msg","success");
        result.put("obj",weatherService.queryWeather(city));
        return result;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值