【Java】spring-cloud 集成webService接口,造成其他controller接口无法访问

pom导入cxf

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

1.新建webservice类

@WebService(name = "LeakageService", // 暴露服务名称
        targetNamespace = "http://webService.webchat.huaxin.com"// 命名空间,一般是接口的包名倒序
)
public interface LeakageService {
    String doSave(String id,String receiveCode);
}

2.新建webservice业务代码实现

@WebService(
        targetNamespace = "http://webService.webchat.huaxin.com", //wsdl命名空间
        serviceName = "LeakageService",                 //portType名称 客户端生成代码时 为接口名称
        endpointInterface = "com.huaxin.webchat.webService.LeakageService")//指定发布
public class LeakageServiceImpl implements LeakageService {
 public String doSave(String id, String receiveCode)
 {
   ....//具体业务代码 省略
 }
}

3.新建CxfConfig

package com.huaxin.webchat.webService.webServiceConfig;


import com.huaxin.webchat.webService.LeakageService;
import com.huaxin.webchat.webService.impl.LeakageServiceImpl;
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

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

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

    @Bean
    public LeakageService leakageService() {
        return new LeakageServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), leakageService());
        endpoint.publish("/LeakageService");
        return endpoint;

    }
}

启动项目,访问 http://localhost:8089/services/LeakageService?wsdl 返回信息如下
根据postman得到结果,webservice OK
但是会出现如下问题, 访问其他controller接口时,会出现无法访问情况:
造成无法访问的原因是因为 spingboot默认注册的是dispatcherServlet,当手动配置 ServletRegistrationBean后springboot不会再去注册默认的dispatcherServlet.
个人理解:发布 webservice的时候,默认配置***Application 启动类的ServletRegistrationBean 。boot就不注册默认的dispatcher。这里我们需要去重新配置一个dispatcherServlet 在启动类中,让webservice去调用

@Bean
public ServletRegistrationBean restServlet() {
    //注解扫描上下文
    AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
    //项目包名
    applicationContext.scan("com.**.webService");//webservice接口包
    DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/*");
    return registrationBean;
}

备注:本文为转载文章,转载链接为https://blog.csdn.net/wangrongfei136/article/details/110877781?spm=1001.2014.3001.5506

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值