【问题标题】:Why I am not getting error message in postman using Spring Boot Application为什么我在使用 Spring Boot Application 的邮递员中没有收到错误消息
【发布时间】:2020-09-05 15:13:35
【问题描述】:

我希望在无效登录期间在 Postman 中显示“找不到管理员”消息,但我得到了这个 作为邮递员输出

{
    "timestamp": "2020-05-19T13:59:01.172+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "",
    "path": "/api/adminLogin"
}

AdminController.java

    @RestController
    @RequestMapping("/api")
    public class AdminController {

    @Autowired
    UserRepository userRepository; 

    @PostMapping("/adminLogin")
    public User login(@RequestBody User user) throws ResourceNotFoundException {

        User user1 = userRepository.findByUserName(user.getUserName());
        if(user1 != null && user1.getRole().equalsIgnoreCase("admin")) {
            return user1;
        }
        else 
        {
            throw new ResourceNotFoundException("Admin not found");
        }
    }

ResourceNotFoundException.java

@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public ResourceNotFoundException() {
        super();
    }

    public ResourceNotFoundException(String message) {
        super(message);
    }

    public ResourceNotFoundException(String message, Throwable cause) {
        super(message, cause);
    }
}

我的邮递员网址 http://localhost:8989/api/adminLogin

我的管理员登录成功工作正常

【问题讨论】:

  • 您是否正在使用 Postman 进行 GET 或 POST 查询测试?
  • POST 请求@AhmedRebai
  • 如果您添加 POSTMAN 请求 - url、方法会很有用
  • 您确定您的 API 路径:/api/adminLogin 吗?
  • @AhmedRebai 是的。我的登录成功工作正常

标签: java spring-boot exception postman


【解决方案1】:

我通过在 application.properties 文件中包含以下代码解决了这个问题

server.error.include-message = always

【讨论】:

  • 在 application.properties 文件中
【解决方案2】:

使用 ResponseEntity 概括控制器响应是标准模式。

但是,您也可以使用以下方法解决您的特定问题:

在您的 build.gradle / POM.XML 中添加以下依赖项

runtimeOnly('org.springframework.boot:spring-boot-devtools')

pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>

在您的 application.properties 文件中:

server.error.include-stacktrace=never

您将开始看到异常消息。

【讨论】:

    【解决方案3】:

    您可以使用RespoenseEntity 修改您的响应正文和状态码。如果你想返回 JSON 格式,你应该像这样创建一个新类。

    @Data //lombok
    public class ResponseModel {
    
        String message;
    
        public ResponseModel(String message) {
            this.message = message;
        }
    }
    

    你的代码是这样的。

     @PostMapping("/adminLogin")
        public ResponseEntity<?> login(@RequestBody User user) {
    
            User user1 = userRepository.findByUserName(user.getUserName());
            if(user1 != null && user1.getRole().equalsIgnoreCase("admin")) {
                return new ResponseEntity<>(user1, HttpStatus.OK);
            }
            else 
            {
                return new ResponseEntity<>(new ResponseModel("Admin not found"), HttpStatus.NOT_FOUND);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-04-22
      • 1970-01-01
      • 2017-03-13
      • 2012-09-07
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 2019-09-01
      • 2017-01-25
      相关资源
      最近更新 更多