RESOLVE
in a rest api application, the return type of the exception handling method must be ResponseEntityor annotate the method @ResponseBodyso that spring boot can serialize http.
UPDATE
starting class:
@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
@EnableConfigurationProperties
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer{
private static Class<Application> applicationClass= Application.class;
public static void main(String[] args) {
SpringApplication.run(applicationClass, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(applicationClass);
}
}
I used @ControllerAdviceto handle global exception handling in spring loading the start network, and I had such a strange problem.
spring https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc, , @ControllerAdivce . , , , RunTimeException.
:
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
@ExceptionHandler(value = RuntimeException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestEntity handleException(HttpServletRequest req, RuntimeException ex) {
RestEntity restEntity=new RestEntity();
Message message=new Message();
message.setCode(1000);
message.setMessage("Something wrong with the server");
restEntity.setMessage(message);
return restEntity;
}
}
, @ExceptionHandler , GlobalDefaultExceptionHandler.
, . - ?
, , @RestControoler GlobalDefaultExceptionHandler, . ...
?