Skip to content

Commit dc2f003

Browse files
committed
now can define path in controller
1 parent f5f06c0 commit dc2f003

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
package io.avaje.http.api;
22

3-
import java.lang.annotation.Retention;
4-
import java.lang.annotation.Target;
5-
63
import static java.lang.annotation.ElementType.TYPE;
74
import static java.lang.annotation.RetentionPolicy.RUNTIME;
85

6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
99
/**
1010
* Marker annotation for controllers.
1111
*
1212
* <pre>{@code
13-
*
14-
* @Controller
15-
* @Path("/customers")
16-
* class CustomerController {
17-
* ...
18-
* }
13+
* @Controller("/customers")
14+
* class CustomerController {
15+
* ...
16+
* }
1917
*
2018
* }</pre>
2119
*/
22-
@Target(value=TYPE)
23-
@Retention(value=RUNTIME)
20+
@Target(TYPE)
21+
@Retention(RUNTIME)
2422
public @interface Controller {
23+
24+
/** Specify the path mapping request to the controller. */
25+
String value() default "";
2526
}

http-generator-core/src/main/java/io/avaje/http/generator/core/ControllerReader.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package io.avaje.http.generator.core;
22

3-
import io.avaje.http.api.Path;
4-
import io.avaje.http.api.Produces;
5-
import io.swagger.v3.oas.annotations.Hidden;
3+
import static java.util.function.Predicate.not;
4+
5+
import java.lang.annotation.Annotation;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.Optional;
9+
import java.util.Set;
10+
import java.util.TreeSet;
611

712
import javax.lang.model.element.Element;
813
import javax.lang.model.element.ElementKind;
@@ -14,11 +19,11 @@
1419
import javax.lang.model.type.TypeMirror;
1520
import javax.lang.model.util.ElementFilter;
1621
import javax.validation.Valid;
17-
import java.lang.annotation.Annotation;
18-
import java.util.ArrayList;
19-
import java.util.List;
20-
import java.util.Set;
21-
import java.util.TreeSet;
22+
23+
import io.avaje.http.api.Controller;
24+
import io.avaje.http.api.Path;
25+
import io.avaje.http.api.Produces;
26+
import io.swagger.v3.oas.annotations.Hidden;
2227

2328
/**
2429
* Reads the type information for the Controller (bean).
@@ -79,7 +84,7 @@ private List<Element> initInterfaces() {
7984
List<Element> interfaces = new ArrayList<>();
8085
for (TypeMirror anInterface : beanType.getInterfaces()) {
8186
final Element ifaceElement = ctx.asElement(anInterface);
82-
if (ifaceElement.getAnnotation(Path.class) != null) {
87+
if ((ifaceElement.getAnnotation(Path.class) != null) || !ifaceElement.getAnnotation(Controller.class).value().isBlank()) {
8388
interfaces.add(ifaceElement);
8489
}
8590
}
@@ -250,11 +255,13 @@ public List<MethodReader> methods() {
250255
}
251256

252257
public String path() {
253-
Path path = findAnnotation(Path.class);
254-
if (path == null) {
255-
return null;
256-
}
257-
return Util.trimPath(path.value());
258+
259+
return Optional.ofNullable(findAnnotation(Controller.class))
260+
.map(Controller::value)
261+
.filter(not(String::isBlank))
262+
.or(() -> Optional.ofNullable(findAnnotation(Path.class)).map(Path::value))
263+
.map(Util::trimPath)
264+
.orElse(null);
258265
}
259266

260267
public void addImportType(String rawType) {

tests/test-helidon/src/main/java/org/example/FooController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020

21-
@Controller
22-
@Path("/foo")
21+
@Controller("/foo")
2322
public class FooController {
2423

2524
//@Produces("text/plain")

0 commit comments

Comments
 (0)