简体   繁体   English

如何解决此问题,我无法从 Postman 或浏览器访问我的 controller class 方法?

[英]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.

相关问题 为什么在控制器中从邮递员请求中收到空值? - Why am I receiving null from postman request in my controller? 如何直接从其他类的库类中访问方法? Java的 - How can I directly access methods from my library class in other classes? Java 如何从另一个类访问对象方法? - How can I access an objects methods from another class? 如何使用 class 中的选定方法限制我的客户? - How can i restrict my clients with selected methods from the class? 如何防止控制器方法在Chrome浏览器的Spring Boot中运行两次? - How can I prevent controller methods from running twice in spring boot in chrome browser? 如何从我的View类访问资产? - How can I access my Assets from my View class? Minecraft Bukkit:如何从主类访问另一个类的属性/方法? - Minecraft Bukkit: How can I access properties/methods in another class from the main class? 如何从“设置”活动中的“主要活动”访问方法? - How can I access methods from my Main Activity in my Settings activity? 如何使用bankSim类中的Event类方法? - How can I utilize my Event class methods in bankSim class? 如何在邮递员中传递类对象? - How can I pass a class object in postman?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM