Home > Blockchain >  Springboot api not working, 404 not found
Springboot api not working, 404 not found

Time:10-25

I created this simple springboot app:

SpringbootBackendApplication.java

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import controller.CaseFileController;

@SpringBootApplication
@ComponentScan(basePackageClasses = CaseFileController.class)
public class SpringbootBackendApplication {

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

}

and

CaseFileController.java

package controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/v1/")
public class CaseFileController {
    
    @GetMapping("/hello")
    public String helloWorld() {
        return "Hello World";
    }
}

But when i try to access localhost:8080/api/v1/hello i get a 404 not found error, i found another question similar to mine that got fixed by adding component scan in the main app file, but as you see, i added it and still doesnt work, any ideas?

CodePudding user response:

Move your Controller to com.example.demo.controllers package and messing with component scan won't be required.

Spring will automatically scan packages under com.example.demo.

Your ComponentScan directive doesn't work because it needs a package and not a class.

CodePudding user response:

maybe there's a problem in your application.properties

  • Related