File tree Expand file tree Collapse file tree 7 files changed +42
-15
lines changed
http-api/src/main/java/io/avaje/http/api
http-generator-core/src/main/java/io/avaje/http/generator/core
tests/test-javalin-jsonb/src/main/java/org/example/myapp/web Expand file tree Collapse file tree 7 files changed +42
-15
lines changed Original file line number Diff line number Diff line change @@ -80,16 +80,16 @@ The annotation processor will generate controller adapters that can register rou
80
80
There isn't a hard requirement to use Avaje for dependency injection. In the absence of avaje-inject the generated class will use ` @jakarta.inject.Singleton ` or ` @javax.inject.Singleton ` depending on what's on the classpath. Any DI library that can find and wire the generated @Singleton beans can be used. You can even use Dagger2 or Guice to wire the controllers if you so desire.
81
81
82
82
To force the AP to generate with ` @javax.inject.Singleton ` (in the case where you have both jakarta and javax on the classpath), use the compiler arg ` -AuseJavax=true `
83
- ```
84
- <plugin>
85
- <groupId>org.apache.maven.plugins</groupId>
86
- <artifactId>maven-compiler-plugin</artifactId>
87
- <configuration>
88
- <compilerArgs>
89
- <arg>-AuseJavax=true</arg>
90
- </compilerArgs>
91
- </configuration>
92
- </plugin>
83
+ ``` xml
84
+ <plugin >
85
+ <groupId >org.apache.maven.plugins</groupId >
86
+ <artifactId >maven-compiler-plugin</artifactId >
87
+ <configuration >
88
+ <compilerArgs >
89
+ <arg >-AuseJavax=true</arg >
90
+ </compilerArgs >
91
+ </configuration >
92
+ </plugin >
93
93
```
94
94
95
95
### Usage with Javalin
Original file line number Diff line number Diff line change
1
+ package io .avaje .http .api ;
2
+
3
+ import static java .lang .annotation .ElementType .METHOD ;
4
+ import static java .lang .annotation .ElementType .PARAMETER ;
5
+ import static java .lang .annotation .ElementType .TYPE ;
6
+ import static java .lang .annotation .RetentionPolicy .SOURCE ;
7
+
8
+ import java .lang .annotation .Retention ;
9
+ import java .lang .annotation .Target ;
10
+
11
+ /**
12
+ *
13
+ * Add @Valid annotation on a controller/method/BeanParam that we want bean validation to be included for.
14
+ * When we do this controller methods that take a request payload will then have the request bean
15
+ * (populated by JSON payload or form parameters) validated before it is passed to the controller
16
+ * method.
17
+ *
18
+ * When trying to validate a @BeanParam bean, this will need to be placed on the BeanParam type
19
+
20
+ */
21
+ @ Retention (SOURCE )
22
+ @ Target ({METHOD , TYPE , PARAMETER })
23
+ public @interface Valid {}
Original file line number Diff line number Diff line change @@ -162,6 +162,7 @@ private boolean initDocHidden() {
162
162
private boolean initHasValid () {
163
163
164
164
return findAnnotation (JavaxValidPrism ::getOptionalOn ).isPresent ()
165
+ || findAnnotation (JakartaValidPrism ::getOptionalOn ).isPresent ()
165
166
|| findAnnotation (ValidPrism ::getOptionalOn ).isPresent ();
166
167
}
167
168
Original file line number Diff line number Diff line change @@ -129,7 +129,9 @@ private boolean useValidation() {
129
129
}
130
130
final var elementType = typeElement (rawType );
131
131
return elementType != null
132
- && (ValidPrism .isPresent (elementType ) || JavaxValidPrism .isPresent (elementType ));
132
+ && (ValidPrism .isPresent (elementType )
133
+ || JavaxValidPrism .isPresent (elementType )
134
+ || JakartaValidPrism .isPresent (elementType ));
133
135
}
134
136
135
137
private void readAnnotations (Element element , ParamType defaultType ) {
Original file line number Diff line number Diff line change @@ -92,6 +92,7 @@ private Javadoc buildJavadoc(ExecutableElement element) {
92
92
private boolean initValid () {
93
93
return findAnnotation (ValidPrism ::getOptionalOn ).isPresent ()
94
94
|| findAnnotation (JavaxValidPrism ::getOptionalOn ).isPresent ()
95
+ || findAnnotation (JakartaValidPrism ::getOptionalOn ).isPresent ()
95
96
|| superMethodHasValid ();
96
97
}
97
98
Original file line number Diff line number Diff line change 26
26
@ GeneratePrism (value = io .avaje .http .api .OpenAPIResponse .class , publicAccess = true )
27
27
@ GeneratePrism (value = io .avaje .http .api .OpenAPIResponses .class , publicAccess = true )
28
28
@ GeneratePrism (value = io .swagger .v3 .oas .annotations .Hidden .class , publicAccess = true )
29
- @ GeneratePrism (value = javax .validation .Valid .class , name = "JavaxValidPrism" , publicAccess = true )
30
- @ GeneratePrism (value = jakarta .validation .Valid .class , publicAccess = true )
31
29
@ GeneratePrism (value = org .jetbrains .annotations .NotNull .class , publicAccess = true )
32
30
@ GeneratePrism (value = io .avaje .http .api .Client .class , publicAccess = true )
33
31
@ GeneratePrism (value = io .avaje .http .api .Client .Import .class , publicAccess = true )
34
32
@ GeneratePrism (value = io .avaje .http .api .RequestTimeout .class , publicAccess = true )
33
+ @ GeneratePrism (value = javax .validation .Valid .class , name = "JavaxValidPrism" )
34
+ @ GeneratePrism (value = jakarta .validation .Valid .class , name = "JakartaValidPrism" )
35
+ @ GeneratePrism (value = io .avaje .http .api .Valid .class )
35
36
package io .avaje .http .generator .core ;
36
37
37
38
import io .avaje .prism .GeneratePrism ;
Original file line number Diff line number Diff line change 8
8
import java .util .concurrent .CompletableFuture ;
9
9
import java .util .concurrent .Executors ;
10
10
11
- import javax .validation .Valid ;
12
-
13
11
import org .example .myapp .service .MyService ;
14
12
15
13
import io .avaje .http .api .BeanParam ;
23
21
import io .avaje .http .api .Post ;
24
22
import io .avaje .http .api .Produces ;
25
23
import io .avaje .http .api .QueryParam ;
24
+ import io .avaje .http .api .Valid ;
26
25
import io .javalin .http .Context ;
27
26
import io .swagger .v3 .oas .annotations .Hidden ;
28
27
import jakarta .inject .Inject ;
You can’t perform that action at this time.
0 commit comments