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
8080There 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.
8181
8282To 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 >
9393```
9494
9595### 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() {
162162 private boolean initHasValid () {
163163
164164 return findAnnotation (JavaxValidPrism ::getOptionalOn ).isPresent ()
165+ || findAnnotation (JakartaValidPrism ::getOptionalOn ).isPresent ()
165166 || findAnnotation (ValidPrism ::getOptionalOn ).isPresent ();
166167 }
167168
Original file line number Diff line number Diff line change @@ -129,7 +129,9 @@ private boolean useValidation() {
129129 }
130130 final var elementType = typeElement (rawType );
131131 return elementType != null
132- && (ValidPrism .isPresent (elementType ) || JavaxValidPrism .isPresent (elementType ));
132+ && (ValidPrism .isPresent (elementType )
133+ || JavaxValidPrism .isPresent (elementType )
134+ || JakartaValidPrism .isPresent (elementType ));
133135 }
134136
135137 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) {
9292 private boolean initValid () {
9393 return findAnnotation (ValidPrism ::getOptionalOn ).isPresent ()
9494 || findAnnotation (JavaxValidPrism ::getOptionalOn ).isPresent ()
95+ || findAnnotation (JakartaValidPrism ::getOptionalOn ).isPresent ()
9596 || superMethodHasValid ();
9697 }
9798
Original file line number Diff line number Diff line change 2626@ GeneratePrism (value = io .avaje .http .api .OpenAPIResponse .class , publicAccess = true )
2727@ GeneratePrism (value = io .avaje .http .api .OpenAPIResponses .class , publicAccess = true )
2828@ 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 )
3129@ GeneratePrism (value = org .jetbrains .annotations .NotNull .class , publicAccess = true )
3230@ GeneratePrism (value = io .avaje .http .api .Client .class , publicAccess = true )
3331@ GeneratePrism (value = io .avaje .http .api .Client .Import .class , publicAccess = true )
3432@ 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 )
3536package io .avaje .http .generator .core ;
3637
3738import io .avaje .prism .GeneratePrism ;
Original file line number Diff line number Diff line change 88import java .util .concurrent .CompletableFuture ;
99import java .util .concurrent .Executors ;
1010
11- import javax .validation .Valid ;
12-
1311import org .example .myapp .service .MyService ;
1412
1513import io .avaje .http .api .BeanParam ;
2321import io .avaje .http .api .Post ;
2422import io .avaje .http .api .Produces ;
2523import io .avaje .http .api .QueryParam ;
24+ import io .avaje .http .api .Valid ;
2625import io .javalin .http .Context ;
2726import io .swagger .v3 .oas .annotations .Hidden ;
2827import jakarta .inject .Inject ;
You can’t perform that action at this time.
0 commit comments