Why I am not getting error message in postman using Spring Boot Application
Asked Answered
E

4

8

I want "Admin not found" message to display in Postman during invalid login but I am getting this as postman output

{
    "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);
    }
}

My Postman URL http://localhost:8989/api/adminLogin

My admin login success is working fine

Exotic answered 19/5, 2020 at 14:3 Comment(7)
Are you making a GET or a POST query testing with your Postman?Bilbrey
POST request @AhmedRebaiExotic
It would be useful if you add your POSTMAN request - url, methodSchlesien
Are you sure about your API path :/api/adminLogin ?Bilbrey
@AhmedRebai yes. My login success is working fineExotic
This should work! Can you share the github link if possibleDestruct
@JsonCreator public NotFoundException(@JsonProperty("key") String key, @JsonProperty("message") String message) { super(key, message); } can you change your implementation like thisBilbrey
E
14

I solved the problem by including the below code in application.properties file

server.error.include-message = always
Exotic answered 20/8, 2020 at 5:48 Comment(2)
In application.properties fileVotyak
You saved my a$$ when chatgpt couldn't do it. thanks broMoldboard
W
4

Generalizing the Controller response with ResponseEntity is the standard pattern.

However you can solve your specific problem using the below as well:

add the below dependency in your 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>

and in your application.properties file:

server.error.include-stacktrace=never

You will start seeing the exception message.

Weisman answered 9/8, 2020 at 4:40 Comment(0)
A
2

You can use RespoenseEntity to modify your response body and status code. If you want to return JSON format you should create a new class like this.

@Data //lombok
public class ResponseModel {

    String message;

    public ResponseModel(String message) {
        this.message = message;
    }
}

and your code like this.

 @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);
        }
    }
Axseed answered 19/5, 2020 at 17:44 Comment(0)
E
0

This can be solved using 2 ways:

  1. Either use Dev tool dependency in your pom.xml
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
  1. Use the below properties in your application.properties file
#server.error.include-message = always
#server.error.include-stacktrace=always

Here the first property will include an error "message" in your response and the second property will include "trace" in your response.

There is nothing to do with Lombok and ResponseEntity here.

If you are adding only dev tool dependency, no need to add any property as it will include "error" and "stack-trace" by default in your response.

Ethiopic answered 27/2 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.