helidon
helidon copied to clipboard
JSON SerDes problem with jakarta.ws.rs.core.Response and lombok with GraalVM Native Image
Getting {} as a JSON response to a REST endpoint when compiling to native-image
Environment Details
- Helidon Version: 4.0.1
- Helidon MP
- JDK version: Java(TM) SE Runtime Environment Oracle GraalVM 21.0.1+12.1 (build 21.0.1+12-jvmci-23.1-b19)
- OS: Sonoma 14.1.2 on Apple Silicon (M1)
- Docker version (if applicable): N/A
Problem Description
Problem occured as I was exploring the migration to native-image. All of our helidon microservices use lombok for our beans and jakarta.ws.rs.core.Response to construct responses to our REST-based endpoints. When building and running as a java application, everything works fine and the JSON response is correct. When compiling to native image, I get {} as the response. No errors in the stacktrace
Steps to reproduce
- Start with helidon-quickstart-mp for Helidon 4.0.1
- Create below Message2.java bean:
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class Message2 {
private String message;
private String greeting;
}
- Add to GreetResouce.java:
@Path("/msg2/{name}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getMessage2(@PathParam("name") String name) {
return Response.ok(Message2.builder().message("hello "+name+"!").build()).build();
}
- Compile normally in java and run
$ mvn package
...
$ java -jar target/helidon-quickstart-mp.jar
...
...
$ curl localhost:8080/greet/msg2/joe
{"message":"hello joe!"}
- Compile to native image and run
$mvn -Pnative-image install -DskipTests
...
$./target/helidon-quickstart-mp
...
...
$ curl localhost:8080/greet/msg2/joe
{}
This could be a red herring but the issue sounds very similar to this: https://github.com/micronaut-projects/micronaut-core/issues/7179
There are two reasons why this may not work:
- insufficient configuration of reflection for native image - all types that are processed using reflection must be added in
reflect-config.json
(native image specific configuration file), as otherwise they will not work at runtime (e.g. you get an empty object with no field and methods) - some problem related to Lombok - I am sorry, we cannot help if this is the case. We do not use Lombok and we do not support it. Please try to reproduce the issue without lombok usage (if point 1 does not help you)
If you create the following file (assuming Maven project):
src/main/resources/META-INF/native-image/groupId/artifactId/reflect-config.json
with the following content:
[
{
"name": "your.package.Message2",
"allPublicMethods": true
}
]
your problem should be fixed
Thank you for the quick response tomas. Your suggestion worked. I must admit I am new to native images so I was just following and trying to adapt our microservices based on this: https://helidon.io/docs/v4/mp/guides/graalnative
Maybe as feedback, some quick blurb in the documentation can be said about reflection (or maybe a sample file can be added to the quickstart archetype)
Thanks again