英文:
How can I resolve this issue, I am unable to access my controller class methods from Postman or Browser?
问题
以下是翻译好的内容:
我正在运行一个简单的Spring Boot应用程序,在控制台上它成功运行。但是当我尝试使用Postman时,它会返回以下错误消息。
{
"timestamp": "2020-07-26T04:22:15.626+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/loginRegistration/user/"
}
我的项目结构如下图所示...
(图片已省略)
我的主类是
package org.example;
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");
}
}
我的控制器是...
package org.example.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";
}
}
请指导我需要做哪些更改以在Postman上运行。
英文:
I am running a simple Spring boot application, In console its running successfully. But when I am trying by postman it gives this error message.
{
"timestamp": "2020-07-26T04:22:15.626+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/loginRegistration/user/"
}
My Main class is
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....
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.
答案1
得分: 2
> spring应用程序名称不作为上下文路径使用。 您将需要在您的application.properties文件中定义以下属性,以便使用'loginRegistration'作为上下文路径。
server.servlet.context-path=/loginRegistration
现在您可以将其用作/loginRegistration/user/
如果有帮助,请告诉我。谢谢
英文:
> spring application name does not use as context path. You will have to
> define below property in your application.properties file to use
> 'loginRegistration' as your context path.
server.servlet.context-path=/loginRegistration
And now you can use it as /loginRegistration/user/
Let me know if that helps. Thanks
答案2
得分: 0
你必须确保在给定的URL之前提供战争名称...并且查看包应该在你的主类下...如果不是,你必须在主类中使用componentscan添加。
英文:
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
答案3
得分: 0
另一种选择是根据@Jimmy的描述,在你的User类中定义@RequestMapping
,如下所示:
@RestController
@RequestMapping("/loginRegistration/user/")
public class User {
英文:
Another alternative to what @Jimmy described is to define @RequestMapping
in your User class as -
@RestController
@RequestMapping("/loginRegistration/user/")
public class User {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论