在spring cloud中加入camunda rest接口时的一个坑

公司正在搭建微服务架构,使用了阿里巴巴的nacos,同时希望将工作流也作为一项微服务集成进来,供其他微服务调用。
工作流引擎选用了camunda,它提供了rest接口的spring boot start封装,可以很方便的集成。创建一个springboot的module,在pom中加入如下依赖:



    org.springframework.cloud
    spring-cloud-starter-alibaba-nacos-discovery


    org.camunda.bpm.springboot
    camunda-bpm-spring-boot-starter-rest


启动一切正常,然而访问camunda rest接口时却出现了异常:

ERROR 13880 --- [nio-8169-exec-1] c.b.s.b.s.r.CamundaJerseyResourceConfig] : Allocate exception for servlet [org.camunda.bpm.spring.boot.starter.rest.CamundaJerseyResourceConfig]

java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:330) ~[jersey-server-2.27.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.lambda$initialize$1(ApplicationHandler.java:316) ~[jersey-server-2.27.jar:na]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316) ~[jersey-common-2.27.jar:na]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298) ~[jersey-common-2.27.jar:na]
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:256) ~[jersey-common-2.27.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:315) ~[jersey-server-2.27.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.(ApplicationHandler.java:282) ~[jersey-server-2.27.jar:na]
    at org.glassfish.jersey.servlet.WebComponent.(WebComponent.java:335) ~[jersey-container-servlet-core-2.27.jar:na]
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178) ~[jersey-container-servlet-core-2.27.jar:na]
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370) ~[jersey-container-servlet-core-2.27.jar:na]
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1123) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:777) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134) ~[tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_201]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_201]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.16.jar:9.0.16]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201]

经过两三个小时的搜索,终于从 StackOverflow的这个问题 和 这篇文章
) 中得到启发,找到了问题所在:jax-rs1和jax-rs2同时存在产生了冲突
由下面的两张依赖关系图可以看出,camunda-bpm-spring-boot-starter-rest依赖于spring-boot-starter-jersey,后者最终依赖了javax.ws.rs:javax.ws.rs-api:2.1;而nacos所依赖的spring-cloud-starter-netflix-ribbon最终依赖了javax.ws.rs:jsr311-api:1.1.1

在spring cloud中加入camunda rest接口时的一个坑_第1张图片
对jax-rs2的依赖

对jax-rs1的依赖

知道了问题所在,解决起来就很简单了,在nacos依赖中将 jsr311-api排除即可:


    org.springframework.cloud
    spring-cloud-starter-alibaba-nacos-discovery
    
        
            javax.ws.rs
            jsr311-api
        
    

你可能感兴趣的:(在spring cloud中加入camunda rest接口时的一个坑)