Support 404 handling for HttpExchange interfaces

Closes gh-32105
This commit is contained in:
rstoyanchev
2025-07-07 09:12:22 +01:00
parent 340468cf4b
commit 1982c7e020
7 changed files with 307 additions and 27 deletions
@@ -0,0 +1,76 @@
/*
* Copyright 2002-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.client.support;
import org.jspecify.annotations.Nullable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.service.invoker.HttpExchangeAdapter;
import org.springframework.web.service.invoker.HttpExchangeAdapterDecorator;
import org.springframework.web.service.invoker.HttpRequestValues;
/**
* {@code HttpExchangeAdapterDecorator} that suppresses the
* {@link HttpClientErrorException.NotFound} exception raised on a 404 response
* and returns a {@code ResponseEntity} with the status set to
* {@link org.springframework.http.HttpStatus#NOT_FOUND} status, or
* {@code null} from {@link #exchangeForBody}.
*
* @author Rossen Stoyanchev
* @since 7.0
*/
public final class NotFoundRestClientAdapterDecorator extends HttpExchangeAdapterDecorator {
public NotFoundRestClientAdapterDecorator(HttpExchangeAdapter delegate) {
super(delegate);
}
@Override
public <T> @Nullable T exchangeForBody(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
try {
return super.exchangeForBody(values, bodyType);
}
catch (HttpClientErrorException.NotFound ex) {
return null;
}
}
@Override
public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues values) {
try {
return super.exchangeForBodilessEntity(values);
}
catch (HttpClientErrorException.NotFound ex) {
return ResponseEntity.notFound().build();
}
}
@Override
public <T> ResponseEntity<T> exchangeForEntity(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
try {
return super.exchangeForEntity(values, bodyType);
}
catch (HttpClientErrorException.NotFound ex) {
return ResponseEntity.notFound().build();
}
}
}
@@ -140,10 +140,10 @@ public final class HttpServiceProxyFactory {
private final List<HttpServiceArgumentResolver> customArgumentResolvers = new ArrayList<>();
private final List<HttpRequestValues.Processor> requestValuesProcessors = new ArrayList<>();
private @Nullable ConversionService conversionService;
private final List<HttpRequestValues.Processor> requestValuesProcessors = new ArrayList<>();
private @Nullable StringValueResolver embeddedValueResolver;
private Builder() {
@@ -181,6 +181,17 @@ public final class HttpServiceProxyFactory {
return this;
}
/**
* Set the {@link ConversionService} to use where input values need to
* be formatted as Strings.
* <p>By default, this is {@link DefaultFormattingConversionService}.
* @return this same builder instance
*/
public Builder conversionService(ConversionService conversionService) {
this.conversionService = conversionService;
return this;
}
/**
* Register an {@link HttpRequestValues} processor that can further
* customize request values based on the method and all arguments.
@@ -193,17 +204,6 @@ public final class HttpServiceProxyFactory {
return this;
}
/**
* Set the {@link ConversionService} to use where input values need to
* be formatted as Strings.
* <p>By default this is {@link DefaultFormattingConversionService}.
* @return this same builder instance
*/
public Builder conversionService(ConversionService conversionService) {
this.conversionService = conversionService;
return this;
}
/**
* Set the {@link StringValueResolver} to use for resolving placeholders
* and expressions embedded in {@link HttpExchange#url()}.
@@ -43,7 +43,7 @@ public class ReactorHttpExchangeAdapterDecorator extends HttpExchangeAdapterDeco
/**
* Return the wrapped delgate {@code HttpExchangeAdapter}.
* Return the wrapped delegate {@code HttpExchangeAdapter}.
*/
@Override
public ReactorHttpExchangeAdapter getHttpExchangeAdapter() {