1

I'm new to Spring Boot. I'm trying to handle a exception when a value is null. Basically, its a value that I'm suppose to get from another external service (using restTemplate.exchange() method) which is currently down, so a null value gets assigned to that variable. Here's the code for the same service:

        ResponseEntity<AuthenticationResponse> respNode = null;
        try {
            respNode = restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<AuthenticationResponse>() {});
        } catch (Exception e) {
            if (respNode == null) {
                log.info("Unable to reach: {} ", url);
                throw new AuthServerException("Auth Server Not Found");
            }
        }

I created a custom Exception to handle the scenario if the value is null. The Exception is thrown and I'm able to read it in the logs, but I'm not able to handle it.

AuthServerException.java

package com.nokia.sp.module.myVerify.portalapp.exception;

public class AuthServerException extends NullPointerException {
    private static final long serialVersionUID = 1L;

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

Controller:

@Controller
@RequiredArgsConstructor
public class LandingPageController {
    @ExceptionHandler(AuthServerException.class)
    public ModelAndView AuthServerExceptionHandler(AuthServerException e) {
        log.info(e.getMessage());
        ModelAndView mav = new ModelAndView();
        mav.setViewName("auth-server-handler");
        return mav;
    }
}

Am I missing something or have I made any syntax Error? Please do help me with the same. Thank you!


Vansh
  • 29
  • 1
  • It seems as the method of the first code fragment has a `throws AuthServerException` extension. If you try calling that specific method, you somehow have to handle it; thus eliminating the need for an exception handler. If you still want to handle the exception via exceptionhandler: Convert your `AuthServerException` into an unchecked exception (e.g. RuntimeException). – Z-100 Jan 12 '23 at 12:27
  • So you are using a more specific Exception called AuthServerException to catch NullPointerException. Please note that the AuthServerException is more specific. It doesn't encapsulate the NullPointerException. You may want to try to catch the NullPointerException and then do what you intend. In other words if I create a specific exception off of Exception called MyException. Only MyException will be caught, not Exception. Try to catch the exact exception you're looking to control. – Dale Jan 12 '23 at 12:27
  • 1
    Additionally: Get rid of the `@ExceptionHandler(...)` annotation. IMO it's better to implement exception handling globally. [Here's a great tutorial for that](https://www.baeldung.com/exception-handling-for-rest-with-spring). I hope this somehow helped :) – Z-100 Jan 12 '23 at 12:29
  • He shouldn't get rid of the `@ExceptionHandler` but asuming you want to have this applied globally and not only the `LandingPageController` you want to put this in an `@ControllerAdvice` annotated class. Unless this is only relevant for the `LandingPageController` (but that appears a bit empty). – M. Deinum Jan 12 '23 at 12:56

0 Answers0