Mer*_*ch0 5 native jackson lombok spring-boot graalvm
我一直在尝试使用 spring boot 3、graalvm、lombok 和 jpa 创建一个示例项目。当我使用 main 方法执行测试时,一切看起来都很完美。但是当我运行本机二进制文件时遇到问题。
首先我创建二进制文件:
mvn -Pnative native:build
Run Code Online (Sandbox Code Playgroud)
然后我运行该项目:
./target/demo
Run Code Online (Sandbox Code Playgroud)
项目开始正常,但是当我尝试使用curl 创建新记录时:
curl --location --request POST 'localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "test",
"surname": "surname"
}'
Run Code Online (Sandbox Code Playgroud)
我在控制台中看到此错误:
2022-12-21T01:51:48.055+01:00 WARN 105751 --- [nio-8080-exec-2] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.example.demo.dto.ExampleDto]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Builder class `com.example.demo.dto.ExampleDto$ExampleDtoBuilder` does not have build method (name: 'build')
2022-12-21T01:51:48.055+01:00 WARN 105751 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/json;charset=UTF-8' is not supported]
Run Code Online (Sandbox Code Playgroud)
我的 DTO 如下所示:
./target/demo
Run Code Online (Sandbox Code Playgroud)
控制器:
curl --location --request POST 'localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "test",
"surname": "surname"
}'
Run Code Online (Sandbox Code Playgroud)
问题很清楚,lombok 创建的“build”方法有问题。如果我检查生成的源,我可以在正确的位置看到该方法。显然 GraalVM(或 Jackson)没有看到该方法。
我还尝试在 GET 方法中添加一些跟踪,以确保控制器有权访问builder.build()方法并且该方法存在于运行时。(第二条线索让我觉得存在配置问题。
我删除了@Value,@Jacksonized并@Builder放置了@Data一切正常。
任何想法?我应该添加额外的配置吗?
示例项目位于github:https ://github.com/elysrivero99/spring-boot-3-native-demo
过了一会儿我找到了答案。您必须将提示添加为:
@SpringBootApplication
@ImportRuntimeHints(DemoApplication.DemoApplicationRuntimeHints.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
static class DemoApplicationRuntimeHints implements RuntimeHintsRegistrar {
@SneakyThrows
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection()
.registerConstructor(ExampleDto.ExampleDtoBuilder.class.getDeclaredConstructor(), ExecutableMode.INVOKE)
.registerMethod(
ExampleDto.ExampleDtoBuilder.class.getMethod("build"), ExecutableMode.INVOKE);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1639 次 |
| 最近记录: |