ResponseStatusExceptionHandler in web.server.handler (plus related polishing)

This commit is contained in:
Juergen Hoeller
2016-08-30 23:58:14 +02:00
parent 4ef428de28
commit 31c5644691
26 changed files with 49 additions and 47 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -84,9 +84,7 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
initStrategies(applicationContext);
}
protected void initStrategies(ApplicationContext context) {
Map<String, HandlerMapping> mappingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
context, HandlerMapping.class, true, false);
@@ -145,4 +143,4 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
throw new IllegalStateException("No HandlerResultHandler for " + handlerResult.getReturnValue());
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -34,7 +34,6 @@ public interface HandlerAdapter {
/**
* Whether this {@code HandlerAdapter} supports the given {@code handler}.
*
* @param handler handler object to check
* @return whether or not the handler is supported
*/
@@ -42,17 +41,14 @@ public interface HandlerAdapter {
/**
* Handle the request with the given handler.
*
* <p>Implementations are encouraged to handle exceptions resulting from the
* invocation of a handler in order and if necessary to return an alternate
* result that represents an error response.
*
* <p>Furthermore since an async {@code HandlerResult} may produce an error
* later during result handling implementations are also encouraged to
* {@link HandlerResult#setExceptionHandler(Function) set an exception
* handler} on the {@code HandlerResult} so that may also be applied later
* after result handling.
*
* @param exchange current server exchange
* @param handler the selected handler which must have been previously
* checked via {@link #supports(Object)}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -80,10 +80,11 @@ public interface HandlerMapping {
*/
String PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE = HandlerMapping.class.getName() + ".producibleMediaTypes";
/**
* Return a handler for this request.
* @param exchange current server exchange
* @return A {@link Mono} that emits one value or none in case the request
* @return a {@link Mono} that emits one value or none in case the request
* cannot be resolved to a handler
*/
Mono<Object> getHandler(ServerWebExchange exchange);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -31,7 +31,6 @@ public interface HandlerResultHandler {
/**
* Whether this handler supports the given {@link HandlerResult}.
*
* @param result result object to check
* @return whether or not this object can use the given result
*/
@@ -40,11 +39,10 @@ public interface HandlerResultHandler {
/**
* Process the given result modifying response headers and/or writing data
* to the response.
*
* @param exchange current server exchange
* @param result the result from the handling
* @return {@code Mono<Void>} to indicate when request handling is complete.
*/
Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result);
}
}

View File

@@ -1,42 +0,0 @@
/*
* Copyright 2002-2015 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
*
* http://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.reactive;
import reactor.core.publisher.Mono;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.WebExceptionHandler;
import org.springframework.web.server.ServerWebExchange;
/**
* Handle {@link ResponseStatusException} by setting the response status.
*
* @author Rossen Stoyanchev
* @since 5.0
*/
public class ResponseStatusExceptionHandler implements WebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (ex instanceof ResponseStatusException) {
exchange.getResponse().setStatusCode(((ResponseStatusException) ex).getStatus());
return Mono.empty();
}
return Mono.error(ex);
}
}

View File

@@ -1,80 +0,0 @@
/*
* Copyright 2002-2015 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
*
* http://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.reactive;
import java.net.URI;
import java.time.Duration;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Mono;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.MockServerHttpRequest;
import org.springframework.http.server.reactive.MockServerHttpResponse;
import org.springframework.tests.TestSubscriber;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.MockWebSessionManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
/**
* Unit tests for {@link ResponseStatusExceptionHandler}.
*
* @author Rossen Stoyanchev
*/
public class ResponseStatusExceptionHandlerTests {
private ResponseStatusExceptionHandler handler;
private MockServerHttpResponse response;
private ServerWebExchange exchange;
@Before
public void setUp() throws Exception {
this.handler = new ResponseStatusExceptionHandler();
this.response = new MockServerHttpResponse();
this.exchange = new DefaultServerWebExchange(
new MockServerHttpRequest(HttpMethod.GET, new URI("/path")),
this.response,
new MockWebSessionManager());
}
@Test
public void handleException() throws Exception {
Throwable ex = new ResponseStatusException(HttpStatus.BAD_REQUEST, "");
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
assertEquals(HttpStatus.BAD_REQUEST, this.response.getStatusCode());
}
@Test
public void unresolvedException() throws Exception {
Throwable expected = new IllegalStateException();
Mono<Void> mono = this.handler.handle(this.exchange, expected);
TestSubscriber.subscribe(mono)
.assertErrorWith(actual -> assertSame(expected, actual));
}
}

View File

@@ -39,7 +39,7 @@ import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.ResponseStatusExceptionHandler;
import org.springframework.web.server.handler.ResponseStatusExceptionHandler;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;