Answer the question
In order to leave comments, you need to log in
Why is the behavior different when using ControllerAdvice?
Controller advice:
@ControllerAdvice
public class ApiControllerAdvice {
@ExceptionHandler(ObjectNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ApiResponse notFoundError(ObjectNotFoundException exception) {
return new ApiResponse<>(new Error(exception.getMessage()));
}
}
@RestController
@RequestMapping("/api/library")
public class LibraryController {
private LibraryService libraryService;
@RequestMapping(value = "/id/{id}/**", method = RequestMethod.GET)
public ApiResponse<Library> findOne(@PathVariable("id") long id) throws ObjectNotFoundException {
Library library = libraryService.findOne(id);
if (library == null) throw new ObjectNotFoundException("Library with id " + id + " not found");
return new ApiResponse<>(library);
}
<...>
@Autowired
public void setLibraryService(LibraryService libraryService) {
this.libraryService = libraryService;
}
}
{
"timestamp": 1456673434092,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type [java.lang.String] to required type [long]; nested exception is java.lang.NumberFormatException: For input string: \"api\"",
"path": "/api/library/id/3"
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question