FeignClient выдает вместо возврата ResponseEntity с ошибкой http-статус

Поскольку я использую ResponseEntity<T> в качестве возвращаемого значения для моего метода FeignClient, я ожидал, что он вернет ResponseEntity со статусом 400, если это то, что возвращает сервер. Но вместо этого выдает FeignException.

Как я могу получить правильный ResponseEntity вместо исключения из FeignClient?

Вот мой FeignClient:

@FeignClient(value = "uaa", configuration = OauthFeignClient.Conf.class)
public interface OauthFeignClient {

    @RequestMapping(
            value = "/oauth/token",
            method = RequestMethod.POST,
            consumes = MULTIPART_FORM_DATA_VALUE,
            produces = APPLICATION_JSON_VALUE)
    ResponseEntity<OauthTokenResponse> token(Map<String, ?> formParams);

    class Conf {

        @Value("${oauth.client.password}")
        String oauthClientPassword;

        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }

        @Bean
        public Contract feignContract() {
            return new SpringMvcContract();
        }

        @Bean
        public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
            return new BasicAuthRequestInterceptor("web-client", oauthClientPassword);
        }

    }
}

и вот как я его использую:

@PostMapping("/login")
public ResponseEntity<LoginTokenPair> getTokens(@RequestBody @Valid LoginRequest userCredentials) {
    Map<String, String> formData = new HashMap<>();

    ResponseEntity<OauthTokenResponse> response = oauthFeignClient.token(formData);

    //code never reached if contacted service returns a 400
    ...
}

person ch4mp    schedule 25.01.2018    source источник


Ответы (2)


Кстати, решение, которое я дал раньше, работает, но мое первоначальное намерение было плохой идеей: ошибка является ошибкой, и не следует обрабатывать номинальный поток. Создание исключения, как это делает Feign, и обработка его с помощью @ExceptionHandler - лучший способ перейти в мир Spring MVC.

Итак, два решения:

  • добавить @ExceptionHandler для FeignException
  • настройте FeignClient с ErrorDecoder, чтобы преобразовать ошибку в исключение, о котором знает ваш бизнес-уровень (и уже предоставил @ExceptionHandler для)

Я предпочитаю второе решение, потому что структура полученного сообщения об ошибке, вероятно, изменится от клиента к другому, поэтому вы можете извлечь более мелкие данные из этой ошибки с помощью декодирования ошибок для каждого клиента.

FeignClient с conf (извините за шум, вызванный fign-form)

@FeignClient(value = "uaa", configuration = OauthFeignClient.Config.class)
public interface OauthFeignClient {

    @RequestMapping(
            value = "/oauth/token",
            method = RequestMethod.POST,
            consumes = MULTIPART_FORM_DATA_VALUE,
            produces = APPLICATION_JSON_VALUE)
    DefaultOAuth2AccessToken token(Map<String, ?> formParams);

    @Configuration
    class Config {

        @Value("${oauth.client.password}")
        String oauthClientPassword;

        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }

        @Bean
        public Decoder springDecoder() {
            return new ResponseEntityDecoder(new SpringDecoder(messageConverters));
        }

        @Bean
        public Contract feignContract() {
            return new SpringMvcContract();
        }

        @Bean
        public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
            return new BasicAuthRequestInterceptor("web-client", oauthClientPassword);
        }

        @Bean
        public ErrorDecoder uaaErrorDecoder(Decoder decoder) {
            return (methodKey, response) -> {
                try {
                    OAuth2Exception uaaException = (OAuth2Exception) decoder.decode(response, OAuth2Exception.class);
                    return new SroException(
                            uaaException.getHttpErrorCode(),
                            uaaException.getOAuth2ErrorCode(),
                            Arrays.asList(uaaException.getSummary()));

                } catch (Exception e) {
                    return new SroException(
                            response.status(),
                            "Authorization server responded with " + response.status() + " but failed to parse error payload",
                            Arrays.asList(e.getMessage()));
                }
            };
        }
    }
}

Распространенное исключение для бизнеса

public class SroException extends RuntimeException implements Serializable {
    public final int status;

    public final List<String> errors;

    public SroException(final int status, final String message, final Collection<String> errors) {
        super(message);
        this.status = status;
        this.errors = Collections.unmodifiableList(new ArrayList<>(errors));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SroException)) return false;
        SroException sroException = (SroException) o;
        return status == sroException.status &&
                Objects.equals(super.getMessage(), sroException.getMessage()) &&
                Objects.equals(errors, sroException.errors);
    }

    @Override
    public int hashCode() {
        return Objects.hash(status, super.getMessage(), errors);
    }
}

Обработчик ошибок (извлеченный из расширения ResponseEntityExceptionHandler)

@ExceptionHandler({SroException.class})
public ResponseEntity<Object> handleSroException(SroException ex) {
    return new SroError(ex).toResponse();
}

Ответ об ошибке DTO

@XmlRootElement
public class SroError implements Serializable {
    public final int status;

    public final String message;

    public final List<String> errors;

    public SroError(final int status, final String message, final Collection<String> errors) {
        this.status = status;
        this.message = message;
        this.errors = Collections.unmodifiableList(new ArrayList<>(errors));
    }

    public SroError(final SroException e) {
        this.status = e.status;
        this.message = e.getMessage();
        this.errors = Collections.unmodifiableList(e.errors);
    }

    protected SroError() {
        this.status = -1;
        this.message = null;
        this.errors = null;
    }

    public ResponseEntity<Object> toResponse() {
        return new ResponseEntity(this, HttpStatus.valueOf(this.status));
    }

    public ResponseEntity<Object> toResponse(HttpHeaders headers) {
        return new ResponseEntity(this, headers, HttpStatus.valueOf(this.status));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SroError)) return false;
        SroError sroException = (SroError) o;
        return status == sroException.status &&
                Objects.equals(message, sroException.message) &&
                Objects.equals(errors, sroException.errors);
    }

    @Override
    public int hashCode() {

        return Objects.hash(status, message, errors);
    }
}

Имитация использования клиента: обратите внимание на прозрачную обработку ошибок (без попыток / ловушек) благодаря @ControllerAdvice & @ExceptionHandler({SroException.class})

@RestController
@RequestMapping("/uaa")
public class AuthenticationController {
    private static final BearerToken REVOCATION_TOKEN = new BearerToken("", 0L);

    private final OauthFeignClient oauthFeignClient;

    private final int refreshTokenValidity;

    @Autowired
    public AuthenticationController(
            OauthFeignClient oauthFeignClient,
            @Value("${oauth.ttl.refresh-token}") int refreshTokenValidity) {
        this.oauthFeignClient = oauthFeignClient;
        this.refreshTokenValidity = refreshTokenValidity;
    }

    @PostMapping("/login")
    public ResponseEntity<LoginTokenPair> getTokens(@RequestBody @Valid LoginRequest userCredentials) {
        Map<String, String> formData = new HashMap<>();
        formData.put("grant_type", "password");
        formData.put("client_id", "web-client");
        formData.put("username", userCredentials.username);
        formData.put("password", userCredentials.password);
        formData.put("scope", "openid");

        DefaultOAuth2AccessToken response = oauthFeignClient.token(formData);
        return ResponseEntity.ok(new LoginTokenPair(
                new BearerToken(response.getValue(), response.getExpiresIn()),
                new BearerToken(response.getRefreshToken().getValue(), refreshTokenValidity)));
    }

    @PostMapping("/logout")
    public ResponseEntity<LoginTokenPair> revokeTokens() {
        return ResponseEntity
                .ok(new LoginTokenPair(REVOCATION_TOKEN, REVOCATION_TOKEN));
    }

    @PostMapping("/refresh")
    public ResponseEntity<BearerToken> refreshToken(@RequestHeader("refresh_token") String refresh_token) {
        Map<String, String> formData = new HashMap<>();
        formData.put("grant_type", "refresh_token");
        formData.put("client_id", "web-client");
        formData.put("refresh_token", refresh_token);
        formData.put("scope", "openid");

        DefaultOAuth2AccessToken response = oauthFeignClient.token(formData);
        return ResponseEntity.ok(new BearerToken(response.getValue(), response.getExpiresIn()));
    }
}
person ch4mp    schedule 25.01.2018

Итак, глядя на исходный код, кажется, что единственное решение на самом деле использует feign.Response в качестве возвращаемого типа для методов FeignClient и ручное декодирование тела чем-то вроде new ObjectMapper().readValue(response.body().asReader(), clazz) (конечно, с охранником в состоянии 2xx, потому что для статусов ошибок очень вероятно, что тело является описанием ошибки, а не допустимой полезной нагрузкой;).

Это позволяет извлекать и пересылать статус, заголовок, тело и т. Д., Даже если статус не находится в диапазоне 2xx.

Изменить: вот способ пересылки статуса, заголовков и сопоставленного тела JSON (если возможно):

public static class JsonFeignResponseHelper {
    private final ObjectMapper json = new ObjectMapper();

    public <T> Optional<T> decode(Response response, Class<T> clazz) {
        if(response.status() >= 200 && response.status() < 300) {
            try {
                return Optional.of(json.readValue(response.body().asReader(), clazz));
            } catch(IOException e) {
                return Optional.empty();
            }
        } else {
            return Optional.empty();
        }
    }

    public <T, U> ResponseEntity<U> toResponseEntity(Response response, Class<T> clazz, Function<? super T, ? extends U> mapper) {
        Optional<U> payload = decode(response, clazz).map(mapper);

        return new ResponseEntity(
                payload.orElse(null),//didn't find a way to feed body with original content if payload is empty
                convertHeaders(response.headers()),
                HttpStatus.valueOf(response.status()));
    }

    public MultiValueMap<String, String>  convertHeaders(Map<String, Collection<String>> responseHeaders) {
        MultiValueMap<String, String> responseEntityHeaders = new LinkedMultiValueMap<>();
        responseHeaders.entrySet().stream().forEach(e -> 
                responseEntityHeaders.put(e.getKey(), new ArrayList<>(e.getValue())));
        return responseEntityHeaders;
    }
}

который можно использовать следующим образом:

@PostMapping("/login")
public ResponseEntity<LoginTokenPair> getTokens(@RequestBody @Valid LoginRequest userCredentials) throws IOException {
    Response response = oauthFeignClient.token();

    return feignHelper.toResponseEntity(
            response,
            OauthTokenResponse.class,
            oauthTokenResponse -> new LoginTokenPair(
                    new BearerToken(oauthTokenResponse.access_token, oauthTokenResponse.expires_in),
                    new BearerToken(oauthTokenResponse.refresh_token, refreshTokenValidity)));
}

Это сохраняет заголовки и код состояния, но теряется сообщение об ошибке: /

person ch4mp    schedule 25.01.2018