简体   繁体   English

将Spring @Retryable与Jackson json反序列化一起使用

[英]Using Spring @Retryable with Jackson json deserialization

I'm trying to use Spring Retry for Jackson deserialization into a POJO object like so: 我正在尝试使用Spring Retry将Jackson反序列化为POJO对象,如下所示:

@Data
public class myPOJO {
    private String cmpStr = "test";
    private String myStr;

    @Retryable(maxAttempts=3, value=RuntimeException.class, backoff=@Backoff(delay=3000))
    @JsonProperty("jsonElement")
    private void retryableFunc(Map<String, Object> jsonElement) {
        try {
            myStr = jsonElement.get("jsonAttribute");
            if (!Objects.equals(myStr, cmpStr))
                throw new RuntimeException("Bad Response");
        }
        catch (Exception e) {
            throw new RuntimeException("Bad Response");
        }
    }

    @Recover
    private void recover(Exception e) {
        System.out.println("Recover triggered");
    }
}

MyPOJO is instantiated like this: MyPOJO的实例化如下:

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singleTonList(MediaType.APPLICATION_JSON));
String jsonAttribute = restTemplate.exchange(URL, HttpMethod.GET, new HttpEntity<>(headers), myPOJO.class).getBody().getMyStr();

Main app looks like: 主应用看起来像:

@SpringBootApplication
@EnableRetry
public class mainApplication {
    public static void main(String[] args) {
        SpringApplication.run(mainApplication.class, args);
    }
}

JSON response looks like this: JSON响应如下所示:

{
    "jsonElement": {
        "jsonAttribute": "test1"
    }
}

Retry is never triggered, but the exception is thrown: 重试从不触发,但会引发异常:

Error while extracting response for type myPOJO and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Bad Response; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Bad Response

I think you have some basic misunderstanding. 我认为您有一些基本的误解。 Spring Retry can only be applied to beans that Spring manages. Spring Retry只能应用于Spring管理的bean。 Your POJO might be created by a Spring component ( RestTemplate ), it's not a managed @Bean in the application context. 您的POJO可能是由Spring组件( RestTemplate )创建的,它不是应用程序上下文中的托管@Bean

You can't use annotation-based retry on objects that are not managed by the application context. 您不能对不受应用程序上下文管理的对象使用基于注释的重试。

You still didn't show how you are calling the retryableFunc but you should, instead, call your object from some helper bean: 您仍然没有显示如何调用retryableFunc但是应该从一些辅助bean调用对象:

public class MyHelper {

    @Retryable(...)
    public void validateMyPojo(MyPojo pojo, Map<...,...> jsonElement) {
        pojo.retryableFunc(jsonElement);
    }

}

and

@Bean
public MyHelper myHelper() {
    return new MyHelper();
}

Or, you can simply use a RetryTemplate to call the method. 或者,您可以简单地使用RetryTemplate来调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM