前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WebService: SpringBoot集成WebService实践三

WebService: SpringBoot集成WebService实践三

作者头像
Freedom123
发布2024-03-29 13:40:38
1040
发布2024-03-29 13:40:38
举报
文章被收录于专栏:DevOpsDevOps

简介

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

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

一、服务端编写

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

代码语言:javascript
复制
<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

代码语言:javascript
复制
@WebService(targetNamespace = "http://service.webservice2020.ysp.org")
public interface WeatherService {
    @WebMethod
    String queryWeather(@WebParam(name = "city") String city);
}

3、实现类

代码语言:javascript
复制
@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、配置类

代码语言:javascript
复制
@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.Pom文件配置

代码语言:javascript
复制
<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、配置类

代码语言:javascript
复制
@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调用

代码语言:javascript
复制
@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;
    }
}

小结

参考:https://blog.csdn.net/shipaiYang/article/details/106100469

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-03-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 一、服务端编写
  • 二、客户端编写
  • 小结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com