Skip to content

Commit a45a6ce

Browse files
authored
Merge pull request #123 from SentryMan/master
Now can define path in @controller
2 parents f5f06c0 + a9a0705 commit a45a6ce

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ package org.example.hello;
7575

7676
import io.avaje.http.api.Controller;
7777
import io.avaje.http.api.Get;
78-
import io.avaje.http.api.Path;
7978
import java.util.List;
8079

81-
@Path("/widgets")
82-
@Controller
80+
@Controller("/widgets")
8381
public class WidgetController {
8482
private final HelloComponent hello;
8583
public WidgetController(HelloComponent hello) {
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: 23 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,9 @@ 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+
var controller = ifaceElement.getAnnotation(Controller.class);
88+
if (controller != null && !controller.value().isBlank()
89+
|| ifaceElement.getAnnotation(Path.class) != null) {
8390
interfaces.add(ifaceElement);
8491
}
8592
}
@@ -250,11 +257,13 @@ public List<MethodReader> methods() {
250257
}
251258

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

260269
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)