0

您好,我正在使用连接到 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>
4

1 回答 1

0

从评论中,我会说你在部署 api 的 docker 配置有问题,我会做下一个验证:

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

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

希望这会有所帮助。

于 2020-06-22T02:48:56.660 回答