我该如何解决这个问题?我无法从Postman或浏览器访问我的控制器类方法?

huangapple go评论84阅读模式
英文:

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.

{
&quot;timestamp&quot;: &quot;2020-07-26T04:22:15.626+00:00&quot;,
&quot;status&quot;: 404,
&quot;error&quot;: &quot;Not Found&quot;,
&quot;message&quot;: &quot;&quot;,
&quot;path&quot;: &quot;/loginRegistration/user/&quot;

}

My project structure is...
我该如何解决这个问题?我无法从Postman或浏览器访问我的控制器类方法?

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(&quot;Welcome&quot;);
    }
}

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(&quot;/user&quot;)
public class User {
    @Autowired
    UserService userService;

    @RequestMapping(value = &quot;/registration&quot;, method = RequestMethod.POST)
    public String registerUser(@Validated UserDto userdto) throws JsonProcessingException {
        userService.registerUser(userdto);
        return &quot;success&quot;;
    }

    @RequestMapping(value = &quot;/getUserList&quot;, method = RequestMethod.GET)
    public List&lt;Registration&gt; getUserList() {
        List&lt;Registration&gt; list = userService.getUserList();
        return list;

    }

    @RequestMapping(value = &quot;/&quot;)
    public String test() {
        return &quot;testing&quot;;
    }
}

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(&quot;/loginRegistration/user/&quot;)
public class User {

huangapple
  • 本文由 发表于 2020年7月26日 13:13:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63096335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定