Springboot整合cxf后不能访问controller,不能访问接口

newfarhui 2019-10-23

参考版本

springboot 1.4.X  <=========>  cxf-spring-boot-starter-jaxws 3.1.X

springboot 1.5.X  <=========>  cxf-spring-boot-starter-jaxws 3.2.X

成功集成cxf后,发现只有webservice服务可以正常使用,其他请求url全部无法正常访问。然后在cxf 配置文件中 WebServiceCxfCfg.java 更改此方法名:dispatcherServlet 改为 disServlet

//public ServletRegistrationBean dispatcherServlet()  

@Bean
public ServletRegistrationBean disServlet(){

    return new ServletRegistrationBean(new CXFServlet() , "/services/*");
}

即可成功访问其他url

是因为 public ServletRegistrationBean dispatcherServlet() 把默认映射覆盖掉了,把这个名字改掉,控制类方法就能访问了。

整合的关键代码

maven

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-frontend-jaxws</artifactId>
  <version>3.2.6</version>
</dependency>

CxfConfig

package com.xxx.xxx.common;

import com.xxx.transfer.interceptor.IpAddressInInterceptor;
import com.xxx.transfer.webservice.DepartmentService;
import com.xxx.transfer.webservice.MedicalCardService;
import com.xxx.transfer.webservice.impl.DepartmentServiceImpl;
import com.xxx.transfer.webservice.impl.MedicalCardServiceImpl;
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.annotation.Resource;
import javax.xml.ws.Endpoint;

/**
 * @Description: CXF配置类
 * @date 2019/10/1617:39
 */
@Configuration
public class CxfConfig {

    @Resource
    IpAddressInInterceptor ipAddressInInterceptor;

    /**
     * 成功集成cxf后,发现只有webservice服务可以正常使用,其他请求url全部无法正常访问,是因为 public ServletRegistrationBean dispatcherServlet() 把默认映射覆盖掉了,把这个名字改掉,控制类方法就能访问了
     * 修改dispatcherServlet为disServlet
     */
    @Bean
    public ServletRegistrationBean disServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
    }

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

    @Bean
    public MedicalCardService medicalCardService() {
        return new MedicalCardServiceImpl();
    }

    @Bean
    public DepartmentService departmentService() {
        return new DepartmentServiceImpl();
    }

    /**
     * 诊疗卡信息查询
     *
     * @return
     */
    @Bean
    public Endpoint getPatInfo() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), medicalCardService());
        endpoint.getInInterceptors().add(ipAddressInInterceptor);
        endpoint.publish("/pat");
        return endpoint;
    }

    /**
     * 获取挂号科室
     *
     * @return
     */
    @Bean
    public Endpoint getDeptList() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), departmentService());
        //添加校验拦截器
        endpoint.getInInterceptors().add(ipAddressInInterceptor);
        endpoint.publish("/dept");
        return endpoint;
    }
}

service

package com.xxx.xxx.webservice;

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

/**
 * @Description: 获取挂号科室
 * @date 2019/10/1617:35
 */
@WebService(
        // 暴露服务名称
        name = "getDeptList",
        // 命名空间,一般是接口的包名倒序
        targetNamespace = "http://webservice.transfer.webservice.com"
)
public interface DepartmentService {

    /**
     * 获取挂号科室
     */
    public String getDeptList(
            //    xxxx
            @WebParam(name = "xxx") String xxx,
            //    xxx
            @WebParam(name = "xxx") String xxx,
            //    xxx
            @WebParam(name = "xxx") String xxx,
            //    xxx
            @WebParam(name = "xxx") String xxx
    );
}
package com.xxx.xxx.webservice.impl;

import com.xxx.xxx.webservice.DepartmentService;

import javax.jws.WebService;

/**
 * @Description:
 * @date 2019/10/1617:38
 */
@WebService(
        // 与接口中指定的name一致
        serviceName = "getDeptList",
        // 与接口中的命名空间一致,一般是接口的包名倒
        targetNamespace = "http://webservice.transfer.webservice.com",
        // 接口地址
        endpointInterface = "com.xxx.transfer.webservice.DepartmentService"
)
public class DepartmentServiceImpl implements DepartmentService {
    @Override
    public String getDeptList(String guahaofs, String riqi, String guahaobc, String guahaolb) {
        return "success";
    }
}

相关推荐

tengmuxin / 0评论 2017-10-16