0

这是一个简单的类,我试图用@value 映射错误消息。

我没有在属性文件中添加相应的值,因为我设置了默认值

@Slf4j
@Component
public class RestTemplateResponseErrorHandler
        implements ResponseErrorHandler {

    @Value("${invalid.password:test}")
    private String invalidPassword;


    @Override
    public boolean hasError(ClientHttpResponse httpResponse)
            throws IOException {

        return (
                httpResponse.getStatusCode().series() == CLIENT_ERROR
                        || httpResponse.getStatusCode().series() == SERVER_ERROR);
    }

    @Override
    public void handleError(ClientHttpResponse httpResponse)
            throws IOException {

        if (httpResponse.getStatusCode()
                .series() == SERVER_ERROR) {
            log.info("RestTemplateResponseErrorHandler --- SERVER_ERROR");
            // handle SERVER_ERROR
        } else if (httpResponse.getStatusCode()
                .series() == CLIENT_ERROR) {
            log.info("RestTemplateResponseErrorHandler --- CLIENT_ERROR");

            if (httpResponse.getStatusCode() == HttpStatus.BAD_REQUEST)
                throw new UnAuthorisedRequestException(invalidPassword);


        }
    }
}

这里抛出新的 UnAuthorisedRequestException(invalidPassword); 返回空

不确定该类有什么问题,因为它被标记为@Component

@Configuration
public class CommonBeanConfig {

    @Bean
    public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory) {
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(new SSLConnectionSocketFactory(sslSocketFactory, new NoopHostnameVerifier()))
                .build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
        restTemplate.setInterceptors(Collections.singletonList(new ClientLoggingInterceptor()));
        restTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());
        return restTemplate;
    }

}

此类被添加为异常处理程序

4

1 回答 1

2

RestTemplateResponseErrorHandler在您的示例中,它不是由 spring 管理的 - 您“手动”创建它。所以 spring 不能@Value用于它不管理的对象。

尝试以下操作:

@Configuration
public class CommonBeanConfig {

    @Bean
    public RestTemplateResponseErrorHandler restTemplateErrorHandler() {
      return new RestTemplateErrorHandler();
    }
    @Bean
    public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory) {
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(new SSLConnectionSocketFactory(sslSocketFactory, new NoopHostnameVerifier()))
                .build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
        restTemplate.setInterceptors(Collections.singletonList(new ClientLoggingInterceptor()));
        restTemplate.setErrorHandler(restTemplateErrorHandler()); // or inject the parameter into the method public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory, RestTemplateErrorHandler restTemplateErrorHandler) 
        return restTemplate;
    }

}

请注意,由于您已经在使用 java config - 您不必将 @Component 放在类上RestTemplateErrorHandler

如果您确实必须使用@Component并希望通过组件扫描找到并实例化它 - 不要在 java 配置中为错误处理程序创建 bean 并使用我在以下评论中提到的方法restTemplate

@Bean
public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory, RestTemplateErrorHandler restTemplateErrorHandler) {
   ...
}

于 2020-10-13T05:37:44.953 回答