学无先后,达者为师

网站首页 编程语言 正文

springboot 集成webservice

作者:从零学习大数据 更新时间: 2023-10-16 编程语言

maven依赖

 <!--webservice相关jar包-->
        <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.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.1</version>
        </dependency>

webservice config配置

package *;

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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * 注意:
 * org.apache.cxf.Bus
 * org.apache.cxf.bus.spring.SpringBus
 * org.apache.cxf.jaxws.EndpointImpl
 * javax.xml.ws.Endpoint
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private SmsService smsService;

    @Autowired
    private EmailService emailService;

    @Value("${webservice.api.url}")
    private String webserviceApiUrl;

    @Value("${webservice.api.email}")
    private String webserviceApiEmail;


    /**
     * Apache CXF 核心架构是以BUS为核心,整合其他组件。
     * Bus是CXF的主干, 为共享资源提供一个可配置的场所,作用类似于Spring的ApplicationContext,这些共享资源包括
     * WSDl管理器、绑定工厂等。通过对BUS进行扩展,可以方便地容纳自己的资源,或者替换现有的资源。默认Bus实现基于Spring架构,
     * 通过依赖注入,在运行时将组件串联起来。BusFactory负责Bus的创建。默认的BusFactory是SpringBusFactory,对应于默认
     * 的Bus实现。在构造过程中,SpringBusFactory会搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。
     * 根据这些配置文件构建一个ApplicationContext。开发者也可以提供自己的配置文件来定制Bus。
     */
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问
     * 此方法被注释后, 即不改变前缀名(默认是services), wsdl访问地址为 http://127.0.0.1:8080/services/ws/api?wsdl
     * 去掉注释后wsdl访问地址为:http://127.0.0.1:8080/soap/ws/api?wsdl
     * http://127.0.0.1:8080/soap/列出服务列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看实际的服务
     * 新建Servlet记得需要在启动类添加注解:@ServletComponentScan
     *
     * 如果启动时出现错误:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
     * 可能是springboot与cfx版本不兼容。
     * 同时在spring boot2.0.6之后的版本与xcf集成,不需要在定义以下方法,直接在application.properties配置文件中添加:
     * cxf.path=/service(默认是services)
     */
    @Bean
    public ServletRegistrationBean dispatcherServlet2() {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    }


    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), smsService);
        endpoint.setPublishedEndpointUrl(webserviceApiUrl);
        endpoint.publish("/ws/api");
        return endpoint;
    }


    @Bean
    public Endpoint mailEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), emailService);
        endpoint.setPublishedEndpointUrl(webserviceApiEmail);
        endpoint.publish("/ws/api/emailService");
        return endpoint;
    }


}
package *;

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

@WebService(name = "SmsService", targetNamespace = "http://sms.webservice.com")
public interface SmsService {
    @WebMethod
    String saveSmsInfo(@WebParam(name = "data", targetNamespace = "http://sms.webservice.com") String data);

}
package *;

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

/**
 * @auther: wwh
 * @date: 2020-11-11 09:34
 * @description:
 */
@WebService(name = "EmailService", targetNamespace = "http://email.webservice.com")
public interface EmailService {

    @WebMethod
    String saveEmailInfo(@WebParam(name = "data", targetNamespace = "http://email.webservice.com") String data);

}
package *;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * WebService涉及到的有这些 "四解三类 ", 即四个注解,三个类
 * @WebMethod
 * @WebService
 * @WebResult
 * @WebParam
 * SpringBus
 * Endpoint
 * EndpointImpl
 *
 * 一般我们都会写一个接口,然后再写一个实现接口的实现类,但是这不是强制性的
 * @WebService 注解表明是一个webservice服务。
 *      name:对外发布的服务名, 对应于<wsdl:portType name="ServerServiceDemo"></wsdl:portType>
 *      targetNamespace:命名空间,一般是接口的包名倒序, 实现类与接口类的这个配置一定要一致这种错误
 *              Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name xxxx
 *              对应于targetNamespace="http://server.webservice.example.com"
 *      endpointInterface:服务接口全路径(如果是没有接口,直接写实现类的,该属性不用配置), 指定做SEI(Service EndPoint Interface)服务端点接口
 *      serviceName:对应于<wsdl:service name="ServerServiceDemoImplService"></wsdl:service>
 *      portName:对应于<wsdl:port binding="tns:ServerServiceDemoImplServiceSoapBinding" name="ServerServiceDemoPort"></wsdl:port>
 *
 * @WebMethod 表示暴露的服务方法, 这里有接口ServerServiceDemo存在,在接口方法已加上@WebMethod, 所以在实现类中不用再加上,否则就要加上
 *      operationName: 接口的方法名
 *      action: 没发现又什么用处
 *      exclude: 默认是false, 用于阻止将某一继承方法公开为web服务
 *
 * @WebResult 表示方法的返回值
 *      name:返回值的名称
 *      partName:
 *      targetNamespace:
 *      header: 默认是false, 是否将参数放到头信息中,用于保护参数,默认在body中
 *
 * @WebParam
 *       name:接口的参数
 *       partName:
 *       targetNamespace:
 *       header: 默认是false, 是否将参数放到头信息中,用于保护参数,默认在body中
 *       model:WebParam.Mode.IN/OUT/INOUT
 */
@Component
@WebService(name = "EmailService", targetNamespace = "http://email.webservice.com",
        endpointInterface = "*.service.webservice.service.EmailService")
@Slf4j
public class EmailServiceImpl implements EmailService {
    @Autowired
    SmartMsgService smartMsgService;


    @Override
    public String saveEmailInfo(@WebParam(name = "data", targetNamespace = "http://email.webservice.com") String data) {
        try {
            log.info("\n【webservice 发送邮件消息开始】 data:{}",data);
            if(StringUtils.isEmpty(data)){
                throw new RuntimeException("内容不能为空");
            }
            JSONObject smsInfo = JSON.parseObject(data);

            String msg_id =  smartMsgService.sendWebserviceMail(smsInfo);

            log.info("\n【webservice 发送邮件消息成功】 msg_id:{}",msg_id);

            return ResultVO.builder().msg("发送成功").result(true).msg_id(msg_id).build().toJSONStr();
        } catch (RuntimeException e) {
            e.printStackTrace();
            log.error("\n【webservice 发送邮件消息失败】 e:{}",e.getMessage());
            return ResultVO.builder().msg(e.getMessage()).result(false).build().toJSONStr();
        }
    }
}
package *;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * WebService涉及到的有这些 "四解三类 ", 即四个注解,三个类
 * @WebMethod
 * @WebService
 * @WebResult
 * @WebParam
 * SpringBus
 * Endpoint
 * EndpointImpl
 *
 * 一般我们都会写一个接口,然后再写一个实现接口的实现类,但是这不是强制性的
 * @WebService 注解表明是一个webservice服务。
 *      name:对外发布的服务名, 对应于<wsdl:portType name="ServerServiceDemo"></wsdl:portType>
 *      targetNamespace:命名空间,一般是接口的包名倒序, 实现类与接口类的这个配置一定要一致这种错误
 *              Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name xxxx
 *              对应于targetNamespace="http://server.webservice.example.com"
 *      endpointInterface:服务接口全路径(如果是没有接口,直接写实现类的,该属性不用配置), 指定做SEI(Service EndPoint Interface)服务端点接口
 *      serviceName:对应于<wsdl:service name="ServerServiceDemoImplService"></wsdl:service>
 *      portName:对应于<wsdl:port binding="tns:ServerServiceDemoImplServiceSoapBinding" name="ServerServiceDemoPort"></wsdl:port>
 *
 * @WebMethod 表示暴露的服务方法, 这里有接口ServerServiceDemo存在,在接口方法已加上@WebMethod, 所以在实现类中不用再加上,否则就要加上
 *      operationName: 接口的方法名
 *      action: 没发现又什么用处
 *      exclude: 默认是false, 用于阻止将某一继承方法公开为web服务
 *
 * @WebResult 表示方法的返回值
 *      name:返回值的名称
 *      partName:
 *      targetNamespace:
 *      header: 默认是false, 是否将参数放到头信息中,用于保护参数,默认在body中
 *
 * @WebParam
 *       name:接口的参数
 *       partName:
 *       targetNamespace:
 *       header: 默认是false, 是否将参数放到头信息中,用于保护参数,默认在body中
 *       model:WebParam.Mode.IN/OUT/INOUT
 */
@Component
@WebService(name = "SmsService", targetNamespace = "http://sms.webservice.com",
        endpointInterface = "*.service.webservice.service.SmsService")
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Autowired
    SmartMsgService smartMsgService;

    @Override
    public String saveSmsInfo(@WebParam(name = "data", targetNamespace = "http://sms.webservice.com") String data) {
        try {
            log.info("\n【webservice 发送短信消息开始】 data:{}",data);
            if(StringUtils.isEmpty(data)){
                throw new RuntimeException("内容不能为空");
            }
            JSONObject smsInfo = JSON.parseObject(data);

            String msg_id =  smartMsgService.sendWebserviceSms(smsInfo);

            log.info("\n【webservice 发送短信消息成功】 msg_id:{}",msg_id);

            return ResultVO.builder().msg("发送成功").result(true).msg_id(msg_id).build().toJSONStr();
        } catch (RuntimeException e) {
            e.printStackTrace();
            log.error("\n【webservice 发送短信消息失败】 e:{}",e.getMessage());
            return ResultVO.builder().msg(e.getMessage()).result(false).build().toJSONStr();
        }
    }

}

原文链接:https://blog.csdn.net/sinat_17618381/article/details/110128130

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新