我正在使用 VS Code 在 Spring Boot 中编写一些代码。只是出于测试目的以确保一切正常,我正在使用 POSTMAN 和浏览器测试对我的控制器类的 REST API 调用。在我的控制器类中调用我的测试方法时,它给出状态代码“404 Not Found”。该代码在端口号 8080 上运行,我的 REST URL 是“http://localhost:8080/tulip/testing”type=GET。早些时候,我也遇到了 React 项目的错误,其中 vs code 无法运行 npx 命令。问题出在 Windows Defender 防病毒软件上,因为它阻止了连接。但现在我也添加了 vs code 作为排除,但它仍然不起作用。我公司的笔记本电脑上也存在 vs code 问题,但是 Spring Tool Suite 工作正常,Postman 可以在那里拨打电话。有什么解决办法吗?
在 POSTMAN 中输出
{
"timestamp": "2024-05-05T17:08:33.502+00:00",
"status": 404,
"error": "Not Found",
"path": "/tulip/testing"
}
主课
package com.ekart.tulip;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TulipApplication {
public static void main(String[] args) {
SpringApplication.run(TulipApplication.class, args);
}
}
控制器类
package com.ekart.controller;
import org.springframework.web.bind.annotation.RestController;
import com.ekart.dto.UserDTO;
import com.ekart.service.TulipService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
@RestController
@RequestMapping("/tulip")
public class TulipController {
@Autowired
TulipService tulipService;
private static final Logger logger = LoggerFactory.getLogger(TulipController.class);
@GetMapping("/testing")
public String testing() {
return "testing";
}
@PostMapping("/register")
public ResponseEntity<String> register(@RequestBody UserDTO userDTO) {
logger.info("*************register method invoked*****************");
return ResponseEntity.ok(tulipService.register(userDTO));
}
@GetMapping("/login")
public ResponseEntity<String> login(@RequestBody UserDTO userDTO) {
return ResponseEntity.ok(null);
}
}
我希望 Postman 能够在 VS Code 中对我的 Spring Boot 项目进行 REST API 调用。
这可能听起来很愚蠢,但请尝试再次重建您的项目。我尝试根据提供的信息重现问题,唯一可能产生问题的是添加或更改但更改的
@GetMapping
尚未应用于构建。