micronaut 控制器在 nativeimage 中返回空 json

micronaut controller returning empty json in nativeimage

我在 micronaut 中有一个控制器

package com.mascix.controller;

import java.time.LocalDate;

import javax.ws.rs.core.Response;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller("/hello")
public class GreetController {
    @Get
    public Response hello() {
        return Response.ok(new ApplicationInfo("micronaut", LocalDate.now().getYear())).build();
    }
}

@Introspected
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
class ApplicationInfo {
    public ApplicationInfo(String string, int year) {
        this.name = string;
        this.releaseYear = year;
    }

    String name;
    int releaseYear;
}

当我从 jar 中 运行 并且响应是

时,它工作正常
{"name":"micronaut","releaseYear":2022}

当我用 graalvm 构建项目时,它 return 是空的 json

{}

你可以看到完整的代码here。日志中没有错误或警告。我错过了什么?

我需要为 ApplicationInfo 使用@ReflectiveAccess。

package com.mascix.controller;

import java.time.LocalDate;

import javax.ws.rs.core.Response;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;

import io.micronaut.core.annotation.ReflectiveAccess;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller("/hello")
public class GreetController {
    @Get
    public Response hello() {
        return Response.ok(new ApplicationInfo("micronaut", LocalDate.now().getYear())).build();
    }
}

@ReflectiveAccess
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
class ApplicationInfo {
    public ApplicationInfo(String string, int year) {
        this.name = string;
        this.releaseYear = year;
    }

    String name;
    int releaseYear;
}

为了完成这个注释工作,我需要在 pom 中将 micronaut-graal 添加到 annotationProcessorPaths/path。

    <plugins>
      <plugin>
        <groupId>io.micronaut.build</groupId>
        <artifactId>micronaut-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <!-- Uncomment to enable incremental compilation -->
          <!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
          <annotationProcessorPaths combine.children="append">
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-http-validation</artifactId>
              <version>${micronaut.version}</version>
            </path>
            <path>
              <groupId>io.micronaut</groupId>
              <artifactId>micronaut-graal</artifactId>
              <version>${micronaut.version}</version>
            </path>
          </annotationProcessorPaths>
          <compilerArgs>
            <arg>-Amicronaut.processing.group=com.mascix</arg>
            <arg>-Amicronaut.processing.module=demo</arg>
          </compilerArgs>
        </configuration>
      </plugin>
    </plugins>

see git log and more details