controlleradvice在我的springboot应用程序中没有被触发?

62o28rlo  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(257)

实际上,我正在使用springboot在kafka流应用程序中工作。
所以在这里我尝试使用 @ControllerAdvice 但这对我不起作用。
是否可以使用 @ControllerAdvice 在我的申请表里。
仅当错误来自控制器时,此控制器建议才起作用。
注意:我的应用程序中没有任何控制器/rest控制器端点。
我们能用其他方法达到同样的效果吗?
请分享你宝贵的想法!
主流:

@Autowired
private XyzTransformer xyztransformer;

@Bean
public KStream<String, ABC> processMember(@Qualifier("defaultKafkaStreamsBuilder") StreamsBuilder streamsBuilder) {
    try {
        LOGGER.info("streaming started...");
        KStream<String, Xyz> xyStream = streamsBuilder.stream(appProperty.kafkaStreamsTopicInput)                   
                .transformValues(() -> xyztransformer)
        xyStream.to(appProperty.kafkaStreamsTopicOutput);
        return memberStream;
    } catch (Exception ex) {
        LOGGER.error("Exception occurred in Streams " + Arrays.toString(ex.getStackTrace()));
        throw ex;
    }
}

变压器等级:

@Component
public class XyzTransformer implements ValueTransformer<Xyz, Abc> {

    @Override
    public void close() {
    }

    @Override
    public void init(ProcessorContext processorContext) {
    }

    @SneakyThrows
    @Override
    public Abc transform(Xyz data) {
        String[] dataSourceTables = new String[]{"abc"};
        try {
            return Abc.builder()
                    .name(data.getName())
                    .build();
        } catch (Exception ex) {
        System.out.println("catched and throwing");  
        throw new CustomTesException("test 1");
        }
    }
}

控制器建议:

@ControllerAdvice
public class Advice extends ResponseEntityExceptionHandler {

    @ExceptionHandler(NullPointerException.class)
    public final void handleAllExceptions(NullPointerException ex) {
        System.out.println("it is in the handler");
        System.out.println(ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public final void handleAllException(Exception ex) {
        System.out.println("it is in the exception handler");
        System.out.println(ex.getMessage());
    }

    @ExceptionHandler(CustomTesException.class)
    public final void handleAllExceptio(CustomTesException ex) {
        System.out.println("it is in the exception handler");
        System.out.println(ex.getMessage());
    }   
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题