springboot中使用webservice服务

和第三方对接,写了个http接口,然后告诉我需要WebService接口;一猜就是老掉牙的项目,还搞这么麻烦的东西,很久很久以前写过,不过已经忘得差不多了,重新熟悉一下,记录一下吧!

一、添加依赖

        <!--webService相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.7</version>
        </dependency>

二、编写接口和接口实现类

接口

package com.zhh.demo.webservice.service;

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

/**
 * @Description: WebService接口示例
 * @Author: zhaoheng
 * @CreateTime: 2022-11-10  16:29
 */

@WebService(name = "UserService", // 暴露服务名称
        targetNamespace = "http://demo.zhh.com" // 命名空间,随便写,但是客户端需要和这里一致
)
public interface UserService {


    /**
     * 注解@WebParam 无参数声名的写法
     * @param name  姓名
     * @param sex   性别
     * @return
     */
    @WebMethod
    String userList(@WebParam(targetNamespace = "http://demo.zhh.com") String name,
                    @WebParam(targetNamespace = "http://demo.zhh.com")  String sex);

    /**
     *  注解@WebParam 有参数声名的写法
     * @param name  姓名
     * @param sex   性别
     * @return
     */
    @WebMethod
    String userList2(@WebParam(targetNamespace = "http://demo.zhh.com", name = "name") String name,
                     @WebParam(targetNamespace = "http://demo.zhh.com", name = "sex") String sex);

}

实现类

package com.zhh.demo.webservice.service.impl;

import com.zhh.demo.webservice.service.UserService;
import lombok.extern.slf4j.Slf4j;

import javax.jws.WebService;

/**
 * @Description: iam系统状态维护接口
 * @Author: zhaoheng
 * @CreateTime: 2022-11-10  16:39
 */
@WebService(serviceName = "UserService", // 与接口的名称一致
        targetNamespace = "http://demo.zhh.com", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.zhh.demo.webservice.service.UserService" // 接口地址
)
@Slf4j
public class UserServiceImpl implements UserService {

    @Override
    public String userList(String name, String sex) {
        log.info("userList-入参:{},{}",name,sex);
        return "OK";
    }

    @Override
    public String userList2(String name, String sex) {
        log.info("userList2-入参:{},{}",name,sex);
        return "OK";
    }

}

三、配置类

package com.zhh.demo.webservice.config;


import com.zhh.demo.webservice.service.UserService;
import com.zhh.demo.webservice.service.impl.UserServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
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.xml.ws.Endpoint;

/**
 * @Description: WebService配置
 * @Author: zhaoheng
 * @CreateTime: 2022-11-10  16:29
 */
@Configuration
public class WebServiceConfig {

    @Bean
    public ServletRegistrationBean cxfServlet() {
        // url前缀,localhost:8080/demo/ws/user?wsdl
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 业务类注入到容器
     */
    @Bean
    public UserService iamStateService() {
        return new UserServiceImpl();
    }

    /**
     * 注册接口到WebService服务
     * @return
     */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), iamStateService());
        // url地址,localhost:8080/demo/ws/user?wsdl
        endpoint.publish("/user");
        return endpoint;
    }
}

四、结果展示

4.1 浏览器访问

 http://localhost:8080/demo/ws/user?wsdl

 4.2 postMan调用

设置Header   Content-Type=text/xml

请求体Body xml格式

4.2.1 调用 userList 接口

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <userList xmlns="http://demo.zhh.com"> <!-- 接口名称、namespace和后端对应 -->
            <arg0>王婷婷</arg0> <!-- 第1个参数 -->
            <arg1>女</arg1>     <!-- 第2个参数 -->
        </userList>
    </soap:Body>
</soap:Envelope>

 请求成功。

 4.2.2 调用 userList2 接口

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <userList2 xmlns="http://demo.zhh.com"> <!-- 接口名称、namespace和后端对应 -->
        <name>李白</name> <!-- 和后端接口参数名称对应 -->
           <sex>男</sex>
    </userList2>
  </soap:Body>
</soap:Envelope>

 

 请求成功。

可以看出,两个接口,一个通过注解声名了接口参数另一个没声名,客户端请求的时候也是不一样的。如果声名了参数,客户端使用对应的参数名称标签就行,否则就需要用arg0、arg1、arg3标签分别表示第一二三个参数。

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Spring Boot启动WebService时,您可能会遇到一些问题。根据引用的错误信息,您的实现类"com.hy.springboot.webservice.demo.service.impl.MyServiceImpl"没有添加"@WebService"或"@WebServiceProvider"注解。这些注解是必需的,以便Spring Boot能够正确地识别和配置您的WebService服务。 根据引用,在使用Spring Boot创建WebService时,您需要添加相应的WebService依赖。您可以在pom.xml文件添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 根据引用提供的示例,您还需要确保您的Application类上添加了"@RestController"和"@SpringBootApplication"注解,并且在该类注入了Endpoint bean。此外,您可以添加一个用于测试Endpoint的RequestMapping方法。 如果您遵循了以上步骤,但仍然无法启动WebService,请确保您的项目结构和配置正确,并检查是否有其他错误或异常。另外,您可以查看日志文件以获取更多详细的错误信息,以帮助您定位问题所在。 希望以上信息对您有所帮助,祝您成功启动Spring BootWebService!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot使用WebService(简单的使用)](https://blog.csdn.net/weixin_45492007/article/details/99638002)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值