1

Hello I'm working with a Spring boot App connecting to a mongo db, my problem is that my endpoint are not working it seems that they are not found by Component scan. I'm have been crazy because I'm blocked to continue developing and I'm not able to identify the issue. All seems to be ok. I suppose that maybe is something related with the versions, but not pretty sure.

This is my controller:

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;
  }
}

This is my Application class:

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);
    }

}

my 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>
rasilvap
  • 1,771
  • 3
  • 31
  • 70
  • Hello, I just try your code an it works, I could make the request to the endpoints you have. Could you give me a hint about how are you making the request? – CarlosJavier Jun 22 '20 at 02:04
  • just like that: localhost:8080/shorten in a post in postman – rasilvap Jun 22 '20 at 02:09
  • Same here, I'm making a POST to http://localhost:8080/shorten from Postman and it works. Are you getting some error in the log? Could you share the stacktrace? – CarlosJavier Jun 22 '20 at 02:20
  • I'm using docker compose, maybe it could be related? – rasilvap Jun 22 '20 at 02:29
  • Could be, so you are deploying your api inside a docker image? Could you give a detailed explanation of how are you deploying and testing? – CarlosJavier Jun 22 '20 at 02:30
  • Yes, but I've done others similar and it worked without problems so I think it is not related. But not totally sure. – rasilvap Jun 22 '20 at 02:32

1 Answers1

0

From the comments, I would say you have a problem with the docker configuration where you are deploying your api, I would do the next validations:

  1. Check your api is being served in the 8080 inside your docker container.
  2. Check if you have correctly configured your docker container so the 8080 port (or the one used by your api) is open.
  3. Check if (for some strange reason) you don't have another api running in 8080 in the docker container.

Finally, get into the SSH of your container and then (if you are running some UNIX SO) make a CURL request to the url and check the answer.

Hopes this helps.

CarlosJavier
  • 1,005
  • 1
  • 16
  • 29