Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ package org.example.hello;

import io.avaje.http.api.Controller;
import io.avaje.http.api.Get;
import io.avaje.http.api.Path;
import java.util.List;

@Path("/widgets")
@Controller
@Controller("/widgets")
public class WidgetController {
private final HelloComponent hello;
public WidgetController(HelloComponent hello) {
Expand Down
23 changes: 12 additions & 11 deletions http-api/src/main/java/io/avaje/http/api/Controller.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package io.avaje.http.api;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Marker annotation for controllers.
*
* <pre>{@code
*
* @Controller
* @Path("/customers")
* class CustomerController {
* ...
* }
* @Controller("/customers")
* class CustomerController {
* ...
* }
*
* }</pre>
*/
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Target(TYPE)
@Retention(RUNTIME)
public @interface Controller {

/** Specify the path mapping request to the controller. */
String value() default "";
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package io.avaje.http.generator.core;

import io.avaje.http.api.Path;
import io.avaje.http.api.Produces;
import io.swagger.v3.oas.annotations.Hidden;
import static java.util.function.Predicate.not;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
Expand All @@ -14,11 +19,11 @@
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import javax.validation.Valid;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import io.avaje.http.api.Controller;
import io.avaje.http.api.Path;
import io.avaje.http.api.Produces;
import io.swagger.v3.oas.annotations.Hidden;

/**
* Reads the type information for the Controller (bean).
Expand Down Expand Up @@ -79,7 +84,9 @@ private List<Element> initInterfaces() {
List<Element> interfaces = new ArrayList<>();
for (TypeMirror anInterface : beanType.getInterfaces()) {
final Element ifaceElement = ctx.asElement(anInterface);
if (ifaceElement.getAnnotation(Path.class) != null) {
var controller = ifaceElement.getAnnotation(Controller.class);
if (controller != null && !controller.value().isBlank()
|| ifaceElement.getAnnotation(Path.class) != null) {
interfaces.add(ifaceElement);
}
}
Expand Down Expand Up @@ -250,11 +257,13 @@ public List<MethodReader> methods() {
}

public String path() {
Path path = findAnnotation(Path.class);
if (path == null) {
return null;
}
return Util.trimPath(path.value());

return Optional.ofNullable(findAnnotation(Controller.class))
.map(Controller::value)
.filter(not(String::isBlank))
.or(() -> Optional.ofNullable(findAnnotation(Path.class)).map(Path::value))
.map(Util::trimPath)
.orElse(null);
}

public void addImportType(String rawType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import java.util.ArrayList;
import java.util.List;

@Controller
@Path("/foo")
@Controller("/foo")
public class FooController {

//@Produces("text/plain")
Expand Down