Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 331
anonymous Avatar asked May 19 '20 14:05

anonymous


2 Answers

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

server.error.include-message = always
like image 175
anonymous Avatar answered Oct 17 '22 17:10

anonymous


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.

like image 41
Divas Avatar answered Oct 17 '22 17:10

Divas