找不到Spring Boot 404错误消息”:“无可用消息

时间:2020-06-21 23:28:21

标签: java spring-boot rest component-scan

你好,我正在使用连接到mongo db的Spring boot App,我的问题是我的端点无法正常工作,看来组件扫描找不到它们。我一直在疯狂,因为我被阻止继续开发,并且无法确定问题所在。一切似乎还好。我想这可能与版本有关,但不确定。

这是我的控制者:

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>

1 个答案:

答案 0 :(得分:0)

从评论中,我会说您在部署api的docker配置方面有问题,我将进行下一个验证:

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

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

希望这会有所帮助。