In a project which uses Java 1.8, lombok and Spring Boot 2.1.5.RELEASE, created a custom exception which is not showing up anywhere in my logs files or stdout.
pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
RestController:
@Slf4j
@RestController
@RequestMapping("/products")
public class RestController {
private HttpHeaders headers = null;
@Autowired
ProductDao productDao;
public RestController() {
headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
}
@RequestMapping(value = {"/{id}"},
method = RequestMethod.GET,
produces = "APPLICATION/JSON")
public ResponseEntity<Object> getProductByUsingId(@PathVariable(value = "id", required = true) Integer id)
throws IOException, ProductNotFoundException {
if (null == id) {
return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
}
ProductResponse product = productDao.findProductById(id);
if (null == product) {
return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Object>(product, headers, HttpStatus.OK);
}
}
ProductNotFoundException:
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="Product Not Found")
public class ProductNotFoundException extends Exception {
private static final long serialVersionId = 1L;
public ProductNotFoundException(int id) {
super("Product does not exist with id: " + id);
}
}
ControllerAdvice:
@Slf4j
@ControllerAdvice(annotations = RestController.class)
public class ProductControllerAdvice {
@ExceptionHandler(ProductNotFoundException.class)
@ResponseStatus(value= HttpStatus.NOT_FOUND)
public void handleProductNotFoundException() {
log.error("\n\n\tProductNotFoundException handler executed\n");
}
}
When I hit my REST Endpoint:
http://localhost:8080/products/0
It returns an HTTP 404 - which it should.
But I don't see the ERROR anywhere in my logs.log (using grep -rnw logs.log -e 'ERROR'
) file and/or inside stdout?
Also, tried having ProductNotException extend RuntimeException
, and it still didn't work...
What am I possibly doing wrong?