简体   繁体   English

Springboot api 不起作用,找不到 404

[英]Springboot api not working, 404 not found

I created this simple springboot app:我创建了这个简单的 springboot 应用程序:

SpringbootBackendApplication.java 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案例文件控制器.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?但是,当我尝试访问 localhost:8080/api/v1/hello 时,我收到 404 not found 错误,我发现了另一个与我的问题类似的问题,该问题通过在主应用程序文件中添加组件扫描得到了修复,但如您所见,我添加了它仍然不起作用,有什么想法吗?

Your context-path might not be correct.您的上下文路径可能不正确。 Could you check the std out where it should has the printed out of the context path, then the url is like this你能不能检查一下std应该在哪里打印出上下文路径,然后url是这样的

<context-path>/api/v1/hello

也许你的 application.properties 有问题

Move your Controller to com.example.demo.controllers package and messing with component scan won't be required.将您的控制器移动到 com.example.demo.controllers 包,不需要搞乱组件扫描。

Spring will automatically scan packages under com.example.demo. Spring 会自动扫描 com.example.demo 下的包。

Your ComponentScan directive doesn't work because it needs a package and not a class.您的 ComponentScan 指令不起作用,因为它需要一个包而不是一个类。

I suggest you.我建议你。

Remove scanpackages because in spring boot is not neccesary, spring boot automatically scan all classess.删除 scanpackages 因为在 spring boot 中不是必需的,spring boot 会自动扫描所有类。

Change @RequestMapping("/api/v1/") to @RequestMapping("/api/v1")将@RequestMapping("/api/v1/") 改为@RequestMapping("/api/v1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM