Add support for custom HTTP status codes (#1012)

This commit is contained in:
Pasichenko Maksym
2024-04-18 15:46:20 +03:00
committed by GitHub
parent ef7581a8f1
commit 596f9b391d
3 changed files with 25 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@ import feign.codec.Decoder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
/**
@@ -36,6 +36,7 @@ import org.springframework.http.ResponseEntity;
*
* @author chad jaros
* @author Olga Maciaszek-Sharma
* @author Maksym Pasichenko
*/
public class ResponseEntityDecoder implements Decoder {
@@ -84,7 +85,7 @@ public class ResponseEntityDecoder implements Decoder {
headers.put(key, new LinkedList<>(response.headers().get(key)));
}
return new ResponseEntity<>((T) instance, headers, HttpStatus.valueOf(response.status()));
return new ResponseEntity<>((T) instance, headers, HttpStatusCode.valueOf(response.status()));
}
}

View File

@@ -32,7 +32,7 @@ import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.HttpMessageConverterExtractor;
@@ -42,6 +42,7 @@ import static org.springframework.cloud.openfeign.support.FeignUtils.getHttpHead
/**
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
* @author Maksym Pasichenko
*/
public class SpringDecoder implements Decoder {
@@ -82,8 +83,8 @@ public class SpringDecoder implements Decoder {
}
@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(response.status());
public HttpStatusCode getStatusCode() {
return HttpStatusCode.valueOf(response.status());
}
@Override

View File

@@ -35,6 +35,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
@@ -49,6 +50,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
* @author Szymon Linowski
* @author Maksym Pasichenko
*/
@SpringBootTest(classes = SpringDecoderIntegrationTests.Application.class, webEnvironment = WebEnvironment.RANDOM_PORT,
value = { "spring.application.name=springdecodertest", "spring.jmx.enabled=false" })
@@ -152,11 +154,21 @@ class SpringDecoderIntegrationTests extends FeignClientFactoryBean {
assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
}
@Test
// Issue: https://github.com/spring-cloud/spring-cloud-openfeign/issues/1010
void testResponseEntityWithCustomHttpStatusCode() {
ResponseEntity<Hello> response = testClient().getCustomHttpStatusCodeResponse();
assertThat(response.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(245));
}
protected interface TestClient {
@GetMapping("/helloresponse")
ResponseEntity<Hello> getHelloResponse();
@GetMapping("/hellocustomhttpstatuscode")
ResponseEntity<Hello> getCustomHttpStatusCodeResponse();
@GetMapping("/hellovoid")
ResponseEntity<Void> getHelloVoid();
@@ -229,6 +241,11 @@ class SpringDecoderIntegrationTests extends FeignClientFactoryBean {
return ResponseEntity.ok(new Hello("hello world via response"));
}
@Override
public ResponseEntity<Hello> getCustomHttpStatusCodeResponse() {
return ResponseEntity.status(245).body(new Hello("hello world via response with custom HTTP status code"));
}
@Override
public ResponseEntity<Void> getHelloVoid() {
return ResponseEntity.noContent().header("X-test-header", "myval").build();