Originally posted by **ethanhere** June 10, 2024
In micronaut 3 we have the following code working
io.micronaut.http.HttpRequest> request = ...
HttpHeaders headers = request.getHeaders();
Optional optionalMyAppId = headers.getFirst("id",
MyAppId.class);
final MyAppId appId = optionalMyAppId.orElse(null);
Where MyAppId is a pojo class:
public final class MyAppId {
private final String value;
private MyAppId(String value) {
this.value = value;
}
@JsonCreator
public static MyAppId valueOf(@JsonProperty String value) {
MyAppIdValidator.validateMyAppId(value);
return new MyAppId(value);
}
@JsonCreator
public static AccountId of(@JsonProperty String value) {
return valueOf(value);
}
}
In micronaut 3, it will get the value of "id" header and use MyAppId.of method to create a MyAppId object.
This code no longer works in micronaut 4. I debugged into the code and found in
https://github.com/micronaut-projects/micronaut-core/blob/0118db96962eaf7a83abbcb81c1afd73139ff777/core/src/main/java/io/micronaut/core/convert/DefaultMutableConversionService.java#L237
It will return EmptyAnnotationMetadata and not able to use of method to convert.
Can you advise how to make our original code working?
Discussed in https://github.com/micronaut-projects/micronaut-core/discussions/10899