[英]How can I resolve this issue, I am unable to access my controller class methods from Postman or Browser?
I am running a simple Spring boot application, In console its running successfully.我正在运行一个简单的 Spring 启动应用程序,在控制台中成功运行。 But when I am trying by postman it gives this error message.但是当我通过 postman 尝试时,它会给出此错误消息。
{
"timestamp": "2020-07-26T04:22:15.626+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/loginRegistration/user/"
} }
My project structure is...我的项目结构是...
My Main class is我的主要 class 是
import org.example.controller.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
System.out.println("Welcome");
}
}
My Controller is....我的 Controller 是....
import com.fasterxml.jackson.core.JsonProcessingException;
import org.example.entity.Registration;
import org.example.model.UserDto;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class User {
@Autowired
UserService userService;
@RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registerUser(@Validated UserDto userdto) throws JsonProcessingException {
userService.registerUser(userdto);
return "success";
}
@RequestMapping(value = "/getUserList", method = RequestMethod.GET)
public List<Registration> getUserList() {
List<Registration> list = userService.getUserList();
return list;
}
@RequestMapping(value = "/")
public String test() {
return "testing";
}
}
Please guide me what changes need to be done to run on postman.请指导我需要进行哪些更改才能在 postman 上运行。
spring application name does not use as context path. spring 应用程序名称不用作上下文路径。 You will have to define below property in your application.properties file to use 'loginRegistration' as your context path.您必须在 application.properties 文件中定义以下属性才能使用“loginRegistration”作为上下文路径。
server.servlet.context-path=/loginRegistration
And now you can use it as /loginRegistration/user/现在您可以将其用作 /loginRegistration/user/
Let me know if that helps.让我知道这是否有帮助。 Thanks谢谢
You have to make sure, you providing war name along with your given URL... And see packages should be under you main class... if not u have to add in main class using componentscan您必须确保,您提供战争名称以及给定的 URL ... 并且查看包应该在您的主要 class ... 如果不是,您必须使用组件添加主要 class
Another alternative to what @Jimmy described is to define @RequestMapping
in your User class as - @Jimmy 所描述的另一种替代方法是将@RequestMapping
在您的用户 class 中定义为 -
@RestController
@RequestMapping("/loginRegistration/user/")
public class User {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.