如何将自定义jar的组件用于另一个项目

时间:2019-05-08 05:35:09

标签: java spring-boot exception javabeans executable-jar

我在springboot中使用@ControllerAdvice为自定义异常处理程序创建了一个jar,并在另一个使用maven存储库的项目中使用了该jar。当项目中发生异常时,该jar异常处理程序类不会处理该异常。

如果我在配置中使用@ComponentScan(“ com.test。*”),则会出现错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'globalExceptionHandler': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.test.GlobaExceptionHandler] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]

我的jar文件包含类:


@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
// handle application/json content-type missing
    @Override
    protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
                                                                     HttpHeaders headers, HttpStatus status,
                                                                     WebRequest request) {

        logger.error("<<< handleHttpMediaTypeNotSupported : "+ ex + " url: "+request.getDescription(false));

        String unsupported = "Unsupported content type: " + ex.getContentType();
        String supported = "Supported content types: " + MediaType.toString(ex.getSupportedMediaTypes());

        String url = request.getDescription(false);
        ExceptionResponse exceptionResponse = new ExceptionResponse(unsupported, supported, url);
        globalResponse = new GlobalResponseDto(HttpStatus.METHOD_NOT_ALLOWED.value(), HttpStatus.METHOD_NOT_ALLOWED,
                new Date(), errorStatus, null, exceptionResponse);

        return new ResponseEntity<Object>(globalResponse, new HttpHeaders(), HttpStatus.METHOD_NOT_ALLOWED);
    }
...
// some other exception handlers...
}


Then i used the jar in pom.xml in another springboot project. How to make exception to handle from this class when exception occurs in @RestController class.

Thank you all in advance.

0 个答案:

没有答案