webservice的日常:springboot集成CXF

webservice 的日常:springboot集成CXF

1.springboot集成WS.
2.webservice的测试.

简介

介绍见【springboot集成WS】,文章主要介绍cxf的应用

cxf:

用之前检查下代码的java版本,idea会使用自己默认的java版本。建议java8,@webservice支持1.8,java11后改为@webserviceProvider了。
springboot已经集成好了依赖直接使用:

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

然后进行接口服务的设置:

@WebService(targetNamespace = "http://service.ws.webservice.com", name = "IHelloService")
public interface IHelloService {
    @WebMethod
    @WebResult
    public HelloResponse hello(@WebParam(name = "helloRequest") HelloRequest helloRequest);
}

实现类与接口保持一致:

@Service
@WebService(
        targetNamespace = "http://service.hello.com",
        endpointInterface = "com.hello.service.IMockRseService",
        serviceName = "IHelloService"
)
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
@InInterceptors(interceptors = "com.hello.SpecInterceptor")
public class HelloService implements IHelloService {
    @Override
    public HelloResponse hello(HelloRequest helloRequest) {
        return new HelloResponse();
    }
}

然后进行cxf配置,发布服务:

@Configuration
public class CxfConfig {
    @Resource
    private Bus bus;
   /* 
   // bus注入不进来时可以主动获取
   public Bus getBus() {
    }*/
    @Resource
    private IHelloService iHelloService;
    @Bean
    public Endpoint helloEndpoint(){
        EndpointImpl endpoint = new EndpointImpl(bus, iHelloService);
        endpoint.publish("/IHelloService");
        return endpoint;
    }
}

说明:
@InInterceptors为拦截器,有需要可使用它进行请求拦截处理,比如需要拦截body为空的然后设置namespace:

@Component
public class CxfInterceptor extends AbstractPhaseInterceptor<Message> {

    public CxfInterceptor() {
        super(Phase.RECEIVE);// 接收时处理
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        InputStream inputStream = message.getContent(InputStream.class);
        try {
            if (inputStream != null) {
                HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
                String stringBuffer = request.getRequestURL().toString();
                if (stringBuffer.contains("services")) {
                    String requestStr = IOUtils.toString(inputStream);
                    if (requestStr.contains("<soap:Body/>")) {
                        requestStr = requestStr.replace("<soap:Body/>",
                                "<soap:Body> <ns2: helloNoRequest xmlns:ns2=\"http://service.ws.webservice.com\"/> </soap:Body>");
                    }
                    message.setContent(InputStream.class, new ByteArrayInputStream(requestStr.getBytes()));
                }
            }
        } catch (Exception e) {

        }
    }
}

补充:
如果有参数问题,可以在WebParam中配置targetNamespace = “”
、WebResult中配置name。

然后浏览器
http://localhost:8080/services/IHelloService?wsdl
(http://localhost:8080/services可以查看所有的interface)

效果:
在这里插入图片描述

补充:
1.无请求参数webservice提供:

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "helloNoRequest")
@ResponsePayload
public HelloResponse helloNoRequest() {
    return new HelloResponse();
}

2.bus注入不进来可以使用:

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

githup:
webservice源码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值