|
7 | 7 | import tools.jackson.databind.ValueSerializer; |
8 | 8 | import tools.jackson.databind.module.SimpleModule; |
9 | 9 |
|
| 10 | +/** |
| 11 | + * Jackson module for serializing javalidation types ({@link TemplateString} and {@link ValidationErrors}). |
| 12 | + * <p> |
| 13 | + * This module registers custom serializers to control how validation errors are serialized to JSON. |
| 14 | + * By default, it uses: |
| 15 | + * <ul> |
| 16 | + * <li>{@link TemplateStringSerializer} for {@link TemplateString} - formats templates using {@link TemplateStringFormatter}</li> |
| 17 | + * <li>{@link ValidationErrorsMixIn} for {@link ValidationErrors} - structures errors as {@code {root: [...], fields: {...}}}</li> |
| 18 | + * </ul> |
| 19 | + * <p> |
| 20 | + * <b>Basic usage (default configuration):</b> |
| 21 | + * <pre>{@code |
| 22 | + * ObjectMapper mapper = JsonMapper.builder() |
| 23 | + * .addModule(JavalidationModule.getDefault()) |
| 24 | + * .build(); |
| 25 | + * }</pre> |
| 26 | + * <p> |
| 27 | + * <b>Custom formatter (for internationalization):</b> |
| 28 | + * <pre>{@code |
| 29 | + * JavalidationModule module = JavalidationModule.builder() |
| 30 | + * .withTemplateStringFormatter(myCustomFormatter) |
| 31 | + * .build(); |
| 32 | + * }</pre> |
| 33 | + * <p> |
| 34 | + * <b>Flattened errors (flat array instead of nested structure):</b> |
| 35 | + * <pre>{@code |
| 36 | + * JavalidationModule module = JavalidationModule.builder() |
| 37 | + * .withFlattenedErrors() |
| 38 | + * .build(); |
| 39 | + * }</pre> |
| 40 | + * |
| 41 | + * @see TemplateStringSerializer |
| 42 | + * @see FlattenedErrorsSerializer |
| 43 | + * @see ValidationErrorsMixIn |
| 44 | + */ |
10 | 45 | public class JavalidationModule extends SimpleModule { |
11 | 46 | private final boolean useDefaultSerializer; |
12 | 47 |
|
@@ -34,39 +69,115 @@ public void setupModule(SetupContext context) { |
34 | 69 | } |
35 | 70 | } |
36 | 71 |
|
| 72 | + /** |
| 73 | + * Returns a module instance with default serializers. |
| 74 | + * <p> |
| 75 | + * Equivalent to {@code JavalidationModule.builder().build()}. |
| 76 | + * |
| 77 | + * @return a new module with default configuration |
| 78 | + */ |
37 | 79 | public static JavalidationModule getDefault() { |
38 | 80 | return builder().build(); |
39 | 81 | } |
40 | 82 |
|
| 83 | + /** |
| 84 | + * Returns a new builder for customizing the module configuration. |
| 85 | + * |
| 86 | + * @return a new {@link Builder} instance |
| 87 | + */ |
41 | 88 | public static Builder builder() { |
42 | 89 | return new Builder(); |
43 | 90 | } |
44 | 91 |
|
| 92 | + /** |
| 93 | + * Builder for configuring {@link JavalidationModule} with custom serializers. |
| 94 | + * <p> |
| 95 | + * Use this builder to customize how {@link TemplateString} and {@link ValidationErrors} |
| 96 | + * are serialized to JSON. |
| 97 | + * |
| 98 | + * @see #withTemplateStringFormatter(TemplateStringFormatter) |
| 99 | + * @see #withFlattenedErrors() |
| 100 | + */ |
45 | 101 | public static class Builder { |
46 | 102 | private ValueSerializer<TemplateString> templateStringSerializer = new TemplateStringSerializer(); |
47 | 103 | private @Nullable ValueSerializer<ValidationErrors> validationErrorsSerializer; |
48 | 104 |
|
| 105 | + /** |
| 106 | + * Creates a new builder with default serializers. |
| 107 | + */ |
49 | 108 | public Builder() { |
50 | 109 | } |
51 | 110 |
|
| 111 | + /** |
| 112 | + * Configures a custom formatter for {@link TemplateString} serialization. |
| 113 | + * <p> |
| 114 | + * This is a convenience method that wraps the formatter in a {@link TemplateStringSerializer}. |
| 115 | + * Use this to customize how template messages are formatted (e.g., for i18n). |
| 116 | + * <p> |
| 117 | + * Example: |
| 118 | + * <pre>{@code |
| 119 | + * builder.withTemplateStringFormatter(new MessageSourceTemplateStringFormatter(messageSource, locale)) |
| 120 | + * }</pre> |
| 121 | + * |
| 122 | + * @param formatter the formatter to use (must not be null) |
| 123 | + * @return this builder for method chaining |
| 124 | + * @see #withTemplateStringSerializer(ValueSerializer) |
| 125 | + */ |
52 | 126 | public Builder withTemplateStringFormatter(TemplateStringFormatter formatter) { |
53 | 127 | return withTemplateStringSerializer(new TemplateStringSerializer(formatter)); |
54 | 128 | } |
55 | 129 |
|
| 130 | + /** |
| 131 | + * Configures the module to serialize {@link ValidationErrors} as a flat array of error strings. |
| 132 | + * <p> |
| 133 | + * By default, errors are serialized with nested structure: {@code {root: [...], fields: {...}}}. |
| 134 | + * This method changes the serialization to a simple flat array: {@code ["error1", "error2", ...]}. |
| 135 | + * <p> |
| 136 | + * Example output: |
| 137 | + * <pre>{@code |
| 138 | + * ["Name is required", "Age must be positive"] |
| 139 | + * }</pre> |
| 140 | + * |
| 141 | + * @return this builder for method chaining |
| 142 | + * @see FlattenedErrorsSerializer |
| 143 | + */ |
56 | 144 | public Builder withFlattenedErrors() { |
57 | 145 | return withValidationErrorsSerializer(new FlattenedErrorsSerializer()); |
58 | 146 | } |
59 | 147 |
|
| 148 | + /** |
| 149 | + * Configures a custom serializer for {@link TemplateString}. |
| 150 | + * <p> |
| 151 | + * Use this for full control over template serialization. For most use cases, |
| 152 | + * {@link #withTemplateStringFormatter(TemplateStringFormatter)} is simpler. |
| 153 | + * |
| 154 | + * @param templateStringSerializer the custom serializer (must not be null) |
| 155 | + * @return this builder for method chaining |
| 156 | + */ |
60 | 157 | public Builder withTemplateStringSerializer(ValueSerializer<TemplateString> templateStringSerializer) { |
61 | 158 | this.templateStringSerializer = templateStringSerializer; |
62 | 159 | return this; |
63 | 160 | } |
64 | 161 |
|
| 162 | + /** |
| 163 | + * Configures a custom serializer for {@link ValidationErrors}. |
| 164 | + * <p> |
| 165 | + * Use this for full control over error structure serialization. For flattened output, |
| 166 | + * {@link #withFlattenedErrors()} is simpler. |
| 167 | + * |
| 168 | + * @param validationErrorsSerializer the custom serializer (must not be null) |
| 169 | + * @return this builder for method chaining |
| 170 | + */ |
65 | 171 | public Builder withValidationErrorsSerializer(ValueSerializer<ValidationErrors> validationErrorsSerializer) { |
66 | 172 | this.validationErrorsSerializer = validationErrorsSerializer; |
67 | 173 | return this; |
68 | 174 | } |
69 | 175 |
|
| 176 | + /** |
| 177 | + * Builds the configured {@link JavalidationModule}. |
| 178 | + * |
| 179 | + * @return a new module instance with the configured serializers |
| 180 | + */ |
70 | 181 | public JavalidationModule build() { |
71 | 182 | return new JavalidationModule(templateStringSerializer, validationErrorsSerializer); |
72 | 183 | } |
|
0 commit comments