I had a spring native app that makes http call to another service. I compiled the service using native image maven plugin (mvn -Pnative-image package). I was seeing this message when my app tries to make a http call, "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."
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
The solution to the above issue was to add build args "--enable-url-protocols=http" to native image maven plugin as below.
<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>
Doc Ref: https://www.graalvm.org/reference-manual/native-image/NativeImageMavenPlugin/
or if you use springboot maven plugin:
<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.