0

实际上,我正在使用 Spring Boot 在 Kafka 流应用程序中工作。

所以在这里我试图使用全局处理异常,@ControllerAdvice但它对我不起作用。

是否可以@ControllerAdvice在我的应用程序中使用。

此控制器建议是否仅在错误来自控制器时才有效。

注意:我的应用程序中没有任何控制器/休息控制器端点。

我们可以通过其他方式达到同样的效果吗?

请分享您宝贵的想法!

主流:

@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());
    }   
}
4

0 回答 0