graalvm spring本机应用不支持http协议

6gpjuf90  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(487)

我有一个spring本机应用程序,可以对另一个服务进行http调用。我使用本机imagemaven插件(mvn-pnativeimagepackage)编译了该服务。当我的应用程序尝试进行http调用时,我看到了这样一条消息:“支持url协议http,但默认情况下不启用。必须通过将--enable url protocols=http选项添加到本机映像命令来启用它。”

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8081/v1/test": Accessing an URL protocol that was not enabled. The URL protocol http is supported but not enabled by default. It must be enabled by adding the --enable-url-protocols=http option to the native-image command.; nested exception is java.net.MalformedURsampleception: Accessing an URL protocol that was not enabled. The URL protocol http is supported but not enabled by default. It must be enabled by adding the --enable-url-protocols=http option to the native-image command.
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:785) ~[com.app.demo.sample.SampleApplication:5.3.4]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711) ~[com.app.demo.sample.SampleApplication:5.3.4]
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:361) ~[com.app.demo.sample.SampleApplication:5.3.4]
  ----

    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) ~[na:na]
    at java.util.concurrent.ScheduledThreadPoosampleecutor$ScheduledFutureTask.run(ScheduledThreadPoosampleecutor.java:305) ~[na:na]
    at java.util.concurrent.ThreadPoosampleecutor.runWorker(ThreadPoosampleecutor.java:1128) ~[na:na]
    at java.util.concurrent.ThreadPoosampleecutor$Worker.run(ThreadPoosampleecutor.java:628) ~[na:na]
    at java.lang.Thread.run(Thread.java:834) ~[na:na]
    at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:519) ~[na:na]
    at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:192) ~[na:na]
Caused by: java.net.MalformedURsampleception: Accessing an URL protocol that was not enabled. The URL protocol http is supported but not enabled by default. It must be enabled by adding the --enable-url-protocols=http option to the native-image command.
    at com.oracle.svm.core.jdk.JavaNetSubstitutions.unsupported(JavaNetSubstitutions.java:215) ~[na:na]
    at com.oracle.svm.core.jdk.JavaNetSubstitutions.getURLStreamHandler(JavaNetSubstitutions.java:172) ~[na:na]
    at java.net.URL.getURLStreamHandler(URL.java:71) ~[na:na]
    at java.net.URL.<init>(URL.java:651) ~[na:na]
    at java.net.URL.fromURI(URL.java:719) ~[na:na]
    at java.net.URI.toURL(URI.java:1116) ~[na:na]
    at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145) ~[na:na]
    at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:124) ~[na:na]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:772) ~[com.app.demo.sample.SampleApplication:5.3.4]
    ... 18 common frames omitted
3hvapo4f

3hvapo4f1#

上述问题的解决方案是将build args“-enable url protocols=http”添加到本机映像maven插件中,如下所示。

<profiles>
        <profile>
            <id>native-image</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.graalvm.nativeimage</groupId>
                        <artifactId>native-image-maven-plugin</artifactId>
                        <version>21.0.0</version>
                        <configuration>
                            <buildArgs>
                                <buildArgss>--enable-url-protocols=http</buildArgss>
                            </buildArgs>
                            <!-- The native image build needs to know the entry point to your application -->
                            <mainClass>com.app.demo.sample.SampleApplication</mainClass>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>native-image</goal>
                                </goals>
                                <phase>package</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

文件参考号:https://www.graalvm.org/reference-manual/native-image/nativeimagemavenplugin/

2skhul33

2skhul332#

或者如果您使用springboot maven插件:

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                    <image>
                        <builder>paketobuildpacks/builder:tiny</builder>
                        <env>
                            <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
                            <BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
                                --enable-url-protocols=http
                            </BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
                        </env>
                    </image>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>

相关问题