首页 > 解决方案 > OpenJDK 11.0.2 的 Spring Boot 2.0 请求映射问题

问题描述

我面临的问题是我无法在点击 URL -> http://localhost:9293/hello时得到任何响应

当我尝试测试这个极简功能时,没有任何问题、错误或任何日志,但没有运气。欢迎任何建议或线索!

如果您需要有关该问题的任何进一步信息,请告诉我!

春季版:2.1.5.RELEASE

OpenJDK 版本:11.0.2

应用程序属性

server.port=9293
spring.mvc.servlet.path=/
server.error.whitelabel.enabled=false

HelloWorldController.java 包 com.sample.springboot.jwt.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
    @RequestMapping({ "/hello" })
    public String firstPage() {
        System.out.println("Hello World");
        return "Hello World";
    }
}

我的主类,SpringBootJwtApplication 包com.sample.springboot.jwt.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootJwtApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootJwtApplication.class, args);
        System.out.println("From the main() method...");
    }
}

这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sample.springboot.rest.jwt.app</groupId>
    <artifactId>Spring-Boot-JWT</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Spring-Boot-JWT</name>
    <description>Demo project for Spring Boot using HTTP GET, POST. Securing the application with Spring Boot Security plus JWT</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <!-- version>1.4.0.RELEASE</version -->
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

EDIT-1:添加我的项目结构-> 在此处输入图像描述

标签: javaspring-boot

解决方案


你可以尝试更改spring.mvc.servlet.pathserver.servlet.context-path.

检查此链接以了解更改

更新

根据您的项目结构,应用程序类不会扫描控制器。因此,您可以将 Application 移动到com.sample.springbootpackage 。


推荐阅读