REST Service doesn't work

Ive tried to create a REST Service with MongoAtlas but get a 404 not Found Exception.

I have the following folder structure

com.example.demo3
                 Demo3Application.java
                 CRUDEMongo.java
                |_test
                      CrudeController.java
package com.example.demo3;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerApi;
import com.mongodb.ServerApiVersion;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.demo3", "com.example.demo3.test"})
public class Demo3Application{
	@Autowired
	Test test;
	public static void main(String[] args) {
		SpringApplication.run(Demo3Application.class, args);
        String connectionString = "mongodb+srv://torben:<password>@cluster0.orswsxw.mongodb.net/?retryWrites=true&w=majority";
		ServerApi serverApi = ServerApi.builder().version(ServerApiVersion.V1).build();
        MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(new ConnectionString(connectionString)).serverApi(serverApi).build();
		System.out.println("Hello");
		try(MongoClient client = MongoClients.create(settings)){
		}
	}

}

package com.example.demo3;

import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.data.repository.CrudRepository;
@EnableMongoRepositories
public interface CRUDEMongo extends CrudRepository<Test, Test>{

}
package com.example.demo3.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RestController
@RequestMapping("/")
public class CrudeController {
  @RequestMapping(value="/te", method= RequestMethod.GET)
  public String requestMethodName() {
    return "Hello";
  }
}

Everytime I try to connect with URL/te on Postman I get a 404 not found Exception. Spring doesn’t seem to found my RestController. I tried everthing. Change folders, ComponentScan but it doesn’t seem to find it.

Tried to set up a REST Service with Spring but get a 404 not found Exception via Postman.

Im definitly connect with the database. I got the following back:

MongoClient with metadata {"driver": {"name": "mongo-java-driver|sync", "version": "4.9.1"}, "os": {"type": "Linux", "name": "Linux", "architecture": "amd64", "version": "5.15.0-78-generic"}, "platform": "Java/Eclipse Adoptium/17.0.7+7"} created with settings MongoClientSettings{readPreference=primary, writeConcern=WriteConcern{w=majority, wTimeout=null ms, journal=null}, retryWrites=true, retryReads=true, readConcern=ReadConcern{level=null}, credential=MongoCredential{mechanism=null, userName='torben', source='admin', password=<hidden>, mechanismProperties=<hidden>}, streamFactoryFactory=null, commandListeners=[], codecRegistry=ProvidersCodecRegistry{codecProviders=[ValueCodecProvider{}, BsonValueCodecProvider{}, DBRefCodecProvider{}, DBObjectCodecProvider{}, DocumentCodecProvider{}, CollectionCodecProvider{}, IterableCodecProvider{}, MapCodecProvider{}, GeoJsonCodecProvider{}, GridFSFileCodecProvider{}, Jsr310CodecProvider{}, JsonObjectCodecProvider{}, BsonCodecProvider{}, EnumCodecProvider{}, com.mongodb.client.model.mql.ExpressionCodecProvider@5cff6b74, com.mongodb.Jep395RecordCodecProvider@627ff1b8]}, loggerSettings=LoggerSettings{maxDocumentLength=1000}, clusterSettings={hosts=[127.0.0.1:27017], srvHost=cluster0.orswsxw.mongodb.net, srvServiceName=mongodb, mode=MULTIPLE, requiredClusterType=REPLICA_SET, requiredReplicaSetName='atlas-12upi3-shard-0', serverSelector='null', clusterListeners='[]', serverSelectionTimeout='30000 ms', localThreshold='30000 ms'}, socketSettings=SocketSettings{connectTimeoutMS=10000, readTimeoutMS=0, receiveBufferSize=0, sendBufferSize=0}, heartbeatSocketSettings=SocketSettings{connectTimeoutMS=10000, readTimeoutMS=10000, receiveBufferSize=0, sendBufferSize=0}, connectionPoolSettings=ConnectionPoolSettings{maxSize=100, minSize=0, maxWaitTimeMS=120000, maxConnectionLifeTimeMS=0, maxConnectionIdleTimeMS=0, maintenanceInitialDelayMS=0, maintenanceFrequencyMS=60000, connectionPoolListeners=[], maxConnecting=2}, serverSettings=ServerSettings{heartbeatFrequencyMS=10000, minHeartbeatFrequencyMS=500, serverListeners='[]', serverMonitorListeners='[]'}, sslSettings=SslSettings{enabled=true, invalidHostNameAllowed=false, context=null}, applicationName='null', compressorList=[], uuidRepresentation=UNSPECIFIED, serverApi=ServerApi{version=V1, deprecationErrors=null, strict=null}, autoEncryptionSettings=null, contextProvider=null}

Hey @Torben_Jox,

Welcome to the MongoDB Community!

  1. Make sure the @RestController annotation on your CrudeController class is from the Spring framework and not some other library. It should be:
import org.springframework.web.bind.annotation.RestController;
  1. Also verify that component scanning is set up correctly to find your controller.
  1. Try adding the @RequestMapping annotation on the CrudeController class in addition to the method:
@RestController
@RequestMapping("/api") 
public class CrudeController {

  @RequestMapping("/te")
  public String requestMethodName() {
    ...
  }

}

Then call /api/te instead of just /te.

  1. Check that your application is running on the correct port that you are calling from Postman. The default is 8080 but you may have configured it differently.
  2. Try accessing the endpoint directly in the browser to see if you get a 404 page or see any errors.

However, looking at the code, it appears that it currently outputs “Hello world”. May I ask how the MongoDB URI is being used within your code?

Regards,
Kushagra