Контроллер Micronaut не обнаружен

#micronaut

#micronaut

Вопрос:

Я использую Micronaut версии 1.0.1 со сборками Maven. Я включил micronaut-inject-java и micronaut-inject-groovy (в дополнение к lombok в зависимости компиляции). Однако во время выполнения, когда я пытаюсь получить доступ к пути контроллера, я вижу сообщение:

 {"_links":{"self":{"href":"/","templated":false}},"message":"Page Not Found"}
  

Вот зависимость maven (извлеченная соответствующая часть), которая поддерживается:

   <repositories>
        <repository>
            <id>jcenter.bintray.com</id>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.micronaut</groupId>
                <artifactId>micronaut-bom</artifactId>
                <version>${micronaut.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-tracing</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-http-client</artifactId>
            <scope>compile</scope>
        </dependency>
        <!--   <dependency>
             <groupId>io.micronaut.configuration</groupId>
             <artifactId>micronaut-mongo-reactive</artifactId>
             <scope>compile</scope>
           </dependency> -->
        <dependency>
            <groupId>io.micronaut.configuration</groupId>
            <artifactId>micronaut-micrometer-core</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut.configuration</groupId>
            <artifactId>micronaut-micrometer-registry-prometheus</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-inject</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-validation</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-runtime</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-http-server-netty</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-management</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-inject-java</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.jaegertracing</groupId>
            <artifactId>jaeger-thrift</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>2.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-all</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>micronaut-inject-groovy</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>${exec.mainClass}</mainClass>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-noverify</argument>
                        <argument>-XX:TieredStopAtLevel=1</argument>
                        <argument>-classpath</argument>
                        <classpath/>
                        <argument>${exec.mainClass}</argument>
                    </arguments>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <compilerId>groovy-eclipse-compiler</compilerId>

                    <!-- The following 2 configs are needed for Lombok to run correctly -->
                    <fork>true</fork>
                    <compilerArguments>
                        <javaAgentClass>lombok.launch.Agent</javaAgentClass>
                    </compilerArguments>
                </configuration>
                <executions>
                    <execution>
                        <id>test-compile</id>
                        <phase>process-test-sources</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <compilerId>groovy-eclipse-compiler</compilerId>

                            <!-- The following 2 configs are needed for Lombok to run correctly -->
                            <fork>true</fork>
                            <compilerArguments>
                                <javaAgentClass>lombok.launch.Agent</javaAgentClass>
                            </compilerArguments>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>

                    <!-- lombok.launch.Agent above is from this plugin -->
                    <dependency>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-maven-plugin</artifactId>
                        <version>1.18.6.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>3.3.0-01</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>2.5.6-02</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-eclipse-compiler</artifactId>
                <version>3.3.0-01</version>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <useFile>false</useFile>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/*Spec.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                        <encoding>UTF-8</encoding>
                        <compilerArgs>
                            <arg>-parameters</arg>
                        </compilerArgs>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${lombok.version}</version>
                            </path>
                            <path>
                                <groupId>io.micronaut.configuration</groupId>
                                <artifactId>micronaut-openapi</artifactId>
                                <version>${micronaut.version}</version>
                            </path>
                            <path>
                                <groupId>io.micronaut</groupId>
                                <artifactId>micronaut-inject-java</artifactId>
                                <version>${micronaut.version}</version>
                            </path>
                            <path>
                                <groupId>io.micronaut</groupId>
                                <artifactId>micronaut-validation</artifactId>
                                <version>${micronaut.version}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                    <executions>
                        <execution>
                            <id>test-compile</id>
                            <goals>
                                <goal>testCompile</goal>
                            </goals>
                            <configuration>
                                <compilerArgs>
                                    <arg>-parameters</arg>
                                </compilerArgs>
                                <annotationProcessorPaths>
                                    <path>
                                        <groupId>org.projectlombok</groupId>
                                        <artifactId>lombok</artifactId>
                                        <version>${lombok.version}</version>
                                    </path>
                                    <path>
                                        <groupId>io.micronaut.configuration</groupId>
                                        <artifactId>micronaut-openapi</artifactId>
                                        <version>${micronaut.version}</version>
                                    </path>
                                    <path>
                                        <groupId>io.micronaut</groupId>
                                        <artifactId>micronaut-inject-java</artifactId>
                                        <version>${micronaut.version}</version>
                                    </path>
                                    <path>
                                        <groupId>io.micronaut</groupId>
                                        <artifactId>micronaut-validation</artifactId>
                                        <version>${micronaut.version}</version>
                                    </path>
                                </annotationProcessorPaths>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <pluginRepositories>
        <pluginRepository>
            <id>bintray</id>
            <name>Groovy Bintray</name>
            <url>https://dl.bintray.com/groovy/maven</url>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
  

Сам контроллер помечен следующим образом:

 package org.test.Service;

import org.test.TestService;
import org.test.TestServiceImpl;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

import javax.inject.Inject;
import java.util.Map;
import java.util.Optional;


@Controller("/api")
public class TestController {

    private TestService testService;

    @Inject
    public TestController(TestService testService) {
        this.testService = testService;
    }

    @Get(value = "/", produces = MediaType.APPLICATION_JSON)
    public HttpResponse<?> getTestPayload(final HttpHeaders headers) {

        String header = headers.getAuthorization().get();

        Optional<String> token = this.testService.getUser(header);

        Map<String, String> responsePayload = TestServiceImpl.getResponsePayload(token);

        if (token.isPresent()) {
            return HttpResponse.accepted().body(responsePayload);
        } else {
            return HttpResponse.unauthorized().body(responsePayload);
        }

    }
}
  

Есть какие-либо подсказки относительно того, что может пойти не так?

Редактировать:

Вот используемая команда curl:

 curl http://localhost:8080/api
{"_links":{"self":{"href":"/api","templated":false}},"message":"Page Not Found"}%
  

Комментарии:

1. Не могли бы вы уточнить, что вы подразумеваете под «когда я пытаюсь получить доступ к пути контроллера»?

2. Почему 1.0.1 текущая версия 1.0.5 ?

3. Извините, я имею в виду, когда я пытаюсь получить доступ к localhost: 8080 . Я также изменил путь к контроллеру на «/ api» и пытаюсь получить доступ к localhost:8080 / api , результат тот же.

4. Только что пробовал с версией 1.0.5. Тот же результат.

5. Не могли бы вы предоставить вызов curl или что-либо еще, что вы используете для вызова конечной точки?

Ответ №1:

Убедитесь, что вы включили процессор аннотаций Kapt и делегируете действия по сборке / запуску IDE для Gradle

Создание вашего первого приложения Micronaut Kotlin