spring-attic / spring-native

Spring Native is now superseded by Spring Boot 3 official native support
https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
Apache License 2.0
2.74k stars 355 forks source link

Errors when building Native, using Gradle #1689

Closed jbiancot closed 2 years ago

jbiancot commented 2 years ago

Hi, I can build and run without problems if I use a JVM, java -jar ...

I am building the project using my Mac Intel "macOS Monterey v12.5", using:

java -version
openjdk version "17.0.4" 2022-07-19
OpenJDK Runtime Environment GraalVM CE 22.2.0 (build 17.0.4+8-jvmci-22.2-b06)
OpenJDK 64-Bit Server VM GraalVM CE 22.2.0 (build 17.0.4+8-jvmci-22.2-b06, mixed mode, sharing)

gradle --version
------------------------------------------------------------
Gradle 7.5.1
------------------------------------------------------------

Build time:   2022-08-05 21:17:56 UTC
Revision:     d1daa0cbf1a0103000b71484e1dbfe096e095918

Kotlin:       1.6.21
Groovy:       3.0.10
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          17.0.4 (GraalVM Community 17.0.4+8-jvmci-22.2-b06)
OS:           Mac OS X 12.5 x86_64

But when using Native, I am getting some errors, I don't know if it is because common names on some Beans? or else?

I have attach the entire project on a ZIP file I have attached 2 text files, one is the build, and the other is the run "errors" shown.

I'll appreciate any help, this is a game changer for me. InhyuckProjectJava17.zip build_native_error.txt nativeBuild.txt

jbiancot commented 2 years ago

Hi @mhalbritter, any help with this?

sdeleuze commented 2 years ago

It seems you miss a proxy hint for javax.servlet.http.HttpServletRequest, for some reason Spring native is not able to infer this one (Spring Boot 3 should be more clever for that kind of use case). For now, I would recommand to specify the missing hint manually, see related documentation.

mhalbritter commented 2 years ago

Hey @jbiancot, the problems are those fields:

@Autowired
private HttpServletRequest httpServletRequest;

As a HttpServletRequest is request scoped, Spring injects a request scoped proxy for the request, which is not detected by Spring native and it fails in native-image.

Please try moving the HttpServletRequest field to parameters of the controller methods.

jbiancot commented 2 years ago

Hi there,

I got rid of the Autowired and the HttpServletRequest.

I defined on the method I need the header this way:

public ResponseEntity<?> addCompany(@RequestHeader("Authorization") String authorization, ...

That works perfect, I was able to Native compiled, and run it !!

I was not sure if I can keep my code, and "add into the hint", I am not familiar with these hints.

Thanks !

jbiancot commented 2 years ago

Keep going with the project: Spring Native, this is a game changer !