{"record":{"id":"0a873a6a88e85636","repo":"quarkusio/quarkus","slug":"the-instance-name-annotation-can-only-be-used","errorCode":null,"errorMessage":"The `{instance.name}` annotation can only be used in methods or classes.","messagePattern":"The `(.+?)` annotation can only be used in methods or classes\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"extensions/resteasy-reactive/rest-jackson/deployment/src/main/java/io/quarkus/resteasy/reactive/jackson/deployment/processor/ResteasyReactiveJacksonProcessor.java","lineNumber":722,"sourceCode":"                    .asParameterizedType()\n                    .arguments()\n                    .stream()\n                    .anyMatch(t -> fieldTypeHasSecureFields(t, indexView, typeToHasSecureField, needToDeleteCache));\n        }\n        return false;\n    }\n\n    private String getTargetId(AnnotationInstance instance) {\n        AnnotationTarget target = instance.target();\n        if (target.kind() == AnnotationTarget.Kind.CLASS) {\n            return getClassId(target.asClass());\n        } else if (target.kind() == AnnotationTarget.Kind.METHOD) {\n            return getMethodId(target.asMethod());\n        } else if (target.kind() == AnnotationTarget.Kind.METHOD_PARAMETER) {\n            return \"request-body;\" + getMethodId(target.asMethodParameter().method());\n        }\n\n        throw new UnsupportedOperationException(String.format(\"The `%s` annotation can only \"\n                + \"be used in methods or classes.\", instance.name()));\n    }\n\n    private String getClassId(ClassInfo classInfo) {\n        return classInfo.name().toString();\n    }\n\n    private String getMethodId(MethodInfo methodInfo) {\n        return getMethodId(methodInfo, methodInfo.declaringClass());\n    }\n\n    private String getMethodId(MethodInfo methodInfo, ClassInfo declaringClassInfo) {\n        List<String> parameterClassNames = new ArrayList<>(methodInfo.parametersCount());\n        for (Type parameter : methodInfo.parameterTypes()) {\n            parameterClassNames.add(parameter.name().toString());\n        }\n        return MethodId.get(methodInfo.name(), declaringClassInfo.name().toString(),\n                parameterClassNames.toArray(EMPTY_STRING_ARRAY));","sourceCodeStart":704,"sourceCodeEnd":740,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/resteasy-reactive/rest-jackson/deployment/src/main/java/io/quarkus/resteasy/reactive/jackson/deployment/processor/ResteasyReactiveJacksonProcessor.java#L704-L740","documentation":"Quarkus keys Jackson features (@JsonView, @CustomSerialization, @CustomDeserialization) by a target identifier computed from the annotation's target: class, method, or method parameter (request body). If the annotation is placed on any other target — most commonly a field — no identifier can be built and the deployment fails with this UnsupportedOperationException naming the annotation class.","triggerScenarios":"Putting @JsonView (or @CustomSerialization/@CustomDeserialization) on a field, enum constant, or other non class/method/parameter element of a JAX-RS resource class.","commonSituations":"Habit from plain Jackson where @JsonView is commonly used on fields/properties; migrating a Spring or standard-Jackson DTO annotation set onto Quarkus REST resources; accidental import of the wrong @JsonView annotation placed at field level in the resource class.","solutions":["Move the annotation from the field to the JAX-RS resource method (or resource class) that returns/consumes the DTO","For per-field view filtering, keep @JsonView on DTO fields (plain Jackson handles that) and reference the view classes via @JsonView on the resource method only","Ensure you import com.fasterxml.jackson.annotation.JsonView and apply it only on the resource endpoints for Quarkus REST features","If you intended request-body customization, annotate the method parameter instead"],"exampleFix":"// before\nclass Resource {\n    @JsonView(Public.class) // wrong: on a field of the resource\n    private User user;\n}\n\n// after\nclass Resource {\n    @GET\n    @JsonView(Public.class)\n    public User get() { ... }\n}","handlingStrategy":"type-guard","validationCode":"for (AnnotatedElement e : annotatedElements) {\n    if ((e instanceof Field) && e.isAnnotationPresent(JsonView.class))\n        throw new IllegalStateException(\"@JsonView on a field of a resource class is not supported; move it to the method\");\n}","typeGuard":"static boolean isSupportedTarget(AnnotatedElement el) {\n    return el instanceof java.lang.reflect.Method || el instanceof Class<?> || isMethodParameter(el);\n}","tryCatchPattern":"try {\n    deploy();\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"can only be used in methods or classes\")) {\n        // relocate the annotation named in the message\n    }\n}","preventionTips":["Only annotate JAX-RS resource classes/methods/parameters with @JsonView and custom (de)serialization annotations","Keep field-level Jackson view annotations inside the DTO, driven from the resource method's @JsonView","Check imports so the right @JsonView annotation is applied in the right place","Review resource classes after migrating from plain Jackson/Spring"],"tags":["jackson","quarkus","jsonview","annotation-target"],"backgroundTag":"unsupported-annotation-target","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}