postMap中hystrix fallback方法的java获取问题

4ioopgfo  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(221)

您好,我正在获取回退方法,在postMap中找不到异常。尽管当我的另一个服务关闭时,它对getMap非常有效。
我正在使用两个服务1)管理 Jmeter 盘和2)类别服务
现在我在管理服务中,想添加,但我想在类别服务关闭时显示回退方法响应。

@Autowired
private GrandParentCategoryServiceImp grandParentService;

@PostMapping("/save/grand-parent-category")
    public RedirectView saveCategory(@ModelAttribute("grandParentCategory")
                                                         GrandParentCategory grandParentCategory){

       GrandParentCategory response = grandParentService.save(grandParentCategory);

        System.out.println("Saving grand category => "+response);

        return new RedirectView("/categories");
    }

我的祖父母CategoryServiceImp.class

@Autowired
    private RestTemplate template;

    @HystrixCommand(fallbackMethod = "saveFallbackForGrandParentCategory")
    @Override
    public GrandParentCategory save(GrandParentCategory grandParentCategory) {

        try{
            // set headers
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);

            // set request body with headers
            HttpEntity<GrandParentCategory> request =
                    new HttpEntity<>(grandParentCategory,headers);

            ResponseEntity<GrandParentCategory> responseEntity
                    = template.postForEntity(RequestURLS.GRAND_PARENT_CATEGORY_STORE_URL,request,GrandParentCategory.class);

            HttpStatus status = responseEntity.getStatusCode();

            System.out.println("body"+responseEntity.getBody());
            if (status == HttpStatus.OK) {
                return responseEntity.getBody();
            }

        }catch (HttpStatusCodeException e){
            if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
                return null;
            }
            throw e;
        }
        return null;

    }

    private GrandParentCategory saveFallbackForGrandParentCategory(){

        return new GrandParentCategory(-1L,"Service is Down");
    }

错误看起来像

There was an unexpected error (type=Internal Server Error, status=500).
fallback method wasn't found: saveFallbackForGrandParentCategory([class com.adminuiservice.dto.GrandParentCategory])
com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found: saveFallbackForGrandParentCategory([class com.adminuiservice.dto.GrandParentCategory])
    at com.netflix.hystrix.contrib.javanica.utils.MethodProvider$FallbackMethodFinder.doFind(MethodProvider.java:190)
    at com.netflix.hystrix.contrib.javanica.utils.MethodProvider$FallbackMethodFinder.find(MethodProvider.java:159)
    at com.netflix.hystrix.contrib.javanica.utils.MethodProvider.getFallbackMethod(MethodProvider.java:73)
    at com.netflix.hystrix.contrib.javanica.utils.MethodProvider.getFallbackMethod(MethodProvider.java:59)
    at com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect.setFallbackMethod(HystrixCommandAspect.java:331)

暂无答案!

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

相关问题