【问题标题】:Spring boot 404 not found Error message": "No message availableSpring boot 404 not found Error message": "No message available
【发布时间】:2020-10-11 18:56:30
【问题描述】:

您好,我正在使用连接到 mongo db 的 Spring boot 应用程序,我的问题是我的端点无法正常工作,似乎组件扫描没有找到它们。我已经疯了,因为我被阻止继续开发并且我无法确定问题所在。一切似乎都很好。我想这可能与版本有关,但不太确定。

这是我的控制器:

package com.mercadolibre.mutants.controllers;

import com.mercadolibre.mutants.entities.Product;
import java.util.concurrent.CompletableFuture;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

  private static final Logger LOGGER = LoggerFactory.getLogger(ProductController.class);
  public static final String SHORTEN_URL_PATH = "/shorten";
  public static final String URL_PATH = "/{id}";

  
  @RequestMapping(value = SHORTEN_URL_PATH, method = RequestMethod.POST)
  @ResponseStatus(HttpStatus.CREATED)
  public CompletableFuture<String> shortenUrl(@RequestBody @Valid final Product shortenUrlDto, HttpServletRequest request) throws Exception {
    LOGGER.info("Url to shorten: " );
    CompletableFuture<String> shortenedUrl = null;
    String localURL = request.getRequestURL().toString();
    //shortenedUrl = shortUrlService.shortenURL(localURL, shortenUrlDto.getUrl());
    LOGGER.info("Shortened url to: " + shortenedUrl);
    return shortenedUrl;
  }

  @GetMapping(URL_PATH)
  public CompletableFuture<String> retrieveOriginalUrl(@PathVariable String id) throws Exception {
    LOGGER.info("short url to redirect: " + id);
    //CompletableFuture<String> redirectUrlString = shortUrlService.getLongURLFromID(id);
    //  LOGGER.info("Original URL: " + redirectUrlString);
    return null;
  }
}

这是我的应用程序类:

package com.mercadolibre.mutants;

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

@SpringBootApplication()
public class MutantsApplication {

    public static void main(String[] args) {
        SpringApplication.run(MutantsApplication.class, args);
    }

}

我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://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.16.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mercadolibre</groupId>
    <artifactId>mutants</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mutants</name>
    <description>Project for Java Challengue</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>2.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Lovelace-SR9</version>
            <type>pom</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

【问题讨论】:

  • 你好,我只是试试你的代码,它可以工作,我可以向你拥有的端点发出请求。你能告诉我你是如何提出请求的吗?
  • 就像这样:localhost:8080/shorten 在邮递员的帖子中
  • 这里也一样,我正在从 Postman 向localhost:8080/shorten 发送 POST 并且它有效。您是否在日志中收到一些错误?你能分享一下堆栈跟踪吗?
  • 我正在使用 docker compose,可能是相关的?
  • 可能是这样,所以您正在将您的 api 部署在 docker 映像中?您能否详细解释一下您是如何部署和测试的?

标签: java spring-boot rest component-scan


【解决方案1】:

从 cmets 来看,我会说您部署 api 的 docker 配置有问题,我会进行下一个验证:

  1. 检查您的 api 是否在 docker 容器内的 8080 中提供服务。
  2. 检查您是否已正确配置 docker 容器,以便打开 8080 端口(或您的 api 使用的端口)。
  3. 检查是否(出于某种奇怪的原因)您没有在 docker 容器中的 8080 中运行另一个 api。

最后,进入容器的 SSH,然后(如果您正在运行一些 UNIX SO)向 url 发出 CURL 请求并检查答案。

希望这会有所帮助。

【讨论】:

  • 但是应用响应了,说明它是在8080端口服务的,问题是响应的是404 not found。
  • 您能分享一下.properties 文件吗?
  • 我的 .properties 为空
  • 你所有的.properties? bootstrap.properties 呢?检查您的 api 是否未在根上下文中运行。您是否尝试在 docker 容器内制作 CURL?
猜你喜欢
  • 2021-06-23
  • 2023-03-05
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
相关资源
最近更新 更多