Setting @JsonProperty value through an @AliasFor in a custom meta-annotation #5039
-
Search before asking
Describe the bugI am trying to use a custom meta-annotation with Version Information2.17.2 Reproduction
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import org.springframework.core.annotation.AliasFor;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.lang.annotation.*;
class Scratch {
public static void main(String[] args) throws JsonProcessingException {
Param param = new Param();
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
System.out.println(objectMapper.writeValueAsString(param));
}
@Target({ElementType.TYPE, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@JacksonAnnotationsInside
@JsonProperty
public @interface ParamAnnotation {
@AliasFor(annotation = JsonProperty.class, attribute = "value")
String name() default "";
}
public static class Param {
@ParamAnnotation(name = "firstName")
public String surname = "Jackson";
}
} Expected behaviorThe field surname should be serialized with the name firstName, meaning Additional contextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
We generally only accept jackson only problems. This problem includes Spring and some of your own annotation experimentation. I don't think it is appropriate to ask Jackson team to debug this for you. Can't you use ChatGPT or Stackoverflow and ask some colleagues to help? |
Beta Was this translation helpful? Give feedback.
-
For support for other 3rd party annotations (like Spring |
Beta Was this translation helpful? Give feedback.
@JacksonAnnotationsInside
handling is very simple: all it does is add other annotations of that type -- exactly as they are -- and uses them if it knows them (i.e. looks for known Jackson annotations). It does not have any translation logic, nor knowledge of 3rd party annotations.For support for other 3rd party annotations (like Spring
@AliasFor
), customAnnotationIntrospector
functionality would be needed.