spring boot - 迫使我在运行时使用jersey client 1.19

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot - 迫使我在运行时使用jersey client 1.19相关的知识,希望对你有一定的参考价值。

我这里有一个棘手的情况。我在用

  • 春天的父母2.0.0.RELEASE
  • 春云版Finchley.BUILD-SNAPSHOT

后者将在运行时提供jersey-client 1.19.1。然而,我必须使用的另一个公司内部库依赖于2.23.2org.glassfish.jersey.client.ClientRequest版本。特别是accept方法

    /**
     * Add new accepted types to the message headers.
     *
     * @param types accepted types to be added.
     */
    public void accept(final String... types) {
        getHeaders().addAll(HttpHeaders.ACCEPT, (Object[]) types);
    }

使用版本javax.ws.rs.core.MultivaluedMap.addAll中不存在的1.19.1

我尝试了什么

第一个想法是排除由jersey-client 1.19.1引入的版本spring cloud eureka,而是依赖于jersey-client 2.23.2。这给出了另一个错误:

java.lang.RuntimeException: java.lang.ClassNotFoundException: com.sun.ws.rs.ext.RuntimeDelegateImpl
    at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:122)
    at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:91)
    at javax.ws.rs.core.UriBuilder.newInstance(UriBuilder.java:69)
    at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:80)
    at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:99)
    at org.glassfish.jersey.client.JerseyWebTarget.<init>(JerseyWebTarget.java:72)
    at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:344)
    at org.glassfish.jersey.client.JerseyClient.target(JerseyClient.java:80)
    org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:790)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:774)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.ClassNotFoundException: com.sun.ws.rs.ext.RuntimeDelegateImpl
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:62)
    at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:155)
    at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:105)

编辑

这是目前的pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupid</groupId>
    <artifactId>artifactid</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>name</name>
    <description>desc</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
        <bot.sdk.version>1.0.1</bot.sdk.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jersey.contribs</groupId>
                    <artifactId>jersey-apache-client4</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>
                        spring-cloud-starter-netflix-ribbon
                    </artifactId>
                </exclusion>
            </exclusions>
            <version>1.4.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>
                        spring-cloud-starter-netflix-ribbon
                    </artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-loader</artifactId>
        </dependency>

        <dependency>
            <groupId>company.groupid</groupId>
            <artifactId>artifactid</artifactId>
            <version>${version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>2.26</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
                 </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
                 </configuration>
              </plugin>
        </plugins>
    </build>
</project>

有了这个pom,我得到了这个新错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method cachingLBClientFactory in org.springframework.cloud.openfeign.ribbon.FeignRibbonClientAutoConfiguration required a bean of type 'org.springframework.cloud.netflix.ribbon.SpringClientFactory' that could not be found.
    - Bean method 'springClientFactory' not loaded because @ConditionalOnClass did not find required class 'com.netflix.ribbon.Ribbon'

谢谢您的帮助!

答案

它不是春季启动,而是spring-cloud-netflix。特别是Eureka客户端是用它构建的,这是一个问题。

请参阅文档的这一部分:http://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.4.3.RELEASE/single/spring-cloud-netflix.html#_eurekaclient_without_jersey

我们建立了一个RestTemplate eureka客户端。要使用它,请在pom.xml中执行以下操作

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-apache-client4</artifactId>
        </exclusion>
    </exclusions>
</dependency>

以上是关于spring boot - 迫使我在运行时使用jersey client 1.19的主要内容,如果未能解决你的问题,请参考以下文章

为啥我在运行简单的 Spring Boot 应用程序时总是得到状态为“404”的 Whitelabel 错误页面

使用响应式框架运行 Spring Boot 应用程序时打开端口

Spring boot集成Redis—RedisTemplate的使用来存储Map集合

在运行时更改 Java Spring Boot Bean

Spring Boot JPA 元模型不能为空!尝试运行 JUnit / 集成测试时

无法从通过intellij运行的spring boot应用程序连接到我在docker上运行的kafka