Add details of the request mapping conditions to mappings endpoint

Closes gh-12080
pull/12068/merge
Andy Wilkinson 7 years ago
parent 2c19257d6d
commit 17c7f027e0

@ -40,7 +40,7 @@ When using Spring MVC, the response contains details of any `DispatcherServlet`
request mappings beneath `contexts.*.mappings.dispatcherServlets`. The following
table describes the structure of this section of the response:
[cols="3,1,3"]
[cols="4,1,2"]
include::{snippets}mappings/response-fields-dispatcher-servlets.adoc[]
@ -76,5 +76,5 @@ When using Spring WebFlux, the response contains details of any `DispatcherHandl
request mappings beneath `contexts.*.mappings.dispatcherHandlers`. The following
table describes the structure of this section of the response:
[cols="3,1,3"]
[cols="4,1,2"]
include::{snippets}mappings/response-fields-dispatcher-handlers.adoc[]

@ -16,7 +16,10 @@
package org.springframework.boot.actuate.autoconfigure.endpoint.web.documentation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
@ -34,10 +37,14 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
@ -77,49 +84,88 @@ public class MappingsEndpointReactiveDocumentationTests
@Test
public void mappings() throws Exception {
this.client.get().uri("/actuator/mappings").exchange().expectStatus().isOk()
.expectBody()
.consumeWith(document("mappings",
responseFields(
beneathPath("contexts.*.mappings.dispatcherHandlers")
.withSubsectionId("dispatcher-handlers"),
fieldWithPath("*").description(
"Dispatcher handler mappings, if any, keyed by "
+ "dispatcher handler bean name."),
fieldWithPath("*.[].handler")
.description("Handler for the mapping."),
fieldWithPath("*.[].predicate")
.description("Predicate for the mapping."),
fieldWithPath("*.[].details").optional()
.type(JsonFieldType.OBJECT)
.description("Additional implementation-specific "
+ "details about the mapping. Optional."),
List<FieldDescriptor> requestMappingConditions = Arrays.asList(
requestMappingConditionField("")
.description("Details of the request mapping conditions.")
.optional(),
requestMappingConditionField(".consumes")
.description("Details of the consumes condition"),
requestMappingConditionField(".consumes.[].mediaType")
.description("Consumed media type."),
requestMappingConditionField(".consumes.[].negated")
.description("Whether the media type is negated."),
requestMappingConditionField(".headers")
.description("Details of the headers condition."),
requestMappingConditionField(".headers.[].name")
.description("Name of the header."),
requestMappingConditionField(".headers.[].value")
.description("Required value of the header, if any."),
requestMappingConditionField(".headers.[].negated")
.description("Whether the value is negated."),
requestMappingConditionField(".methods")
.description("HTTP methods that are handled."),
requestMappingConditionField(".params")
.description("Details of the params condition."),
requestMappingConditionField(".params.[].name")
.description("Name of the parameter."),
requestMappingConditionField(".params.[].value")
.description("Required value of the parameter, if any."),
requestMappingConditionField(".params.[].negated")
.description("Whether the value is negated."),
requestMappingConditionField(".patterns").description(
"Patterns identifying the paths handled by the mapping."),
requestMappingConditionField(".produces")
.description("Details of the produces condition."),
requestMappingConditionField(".produces.[].mediaType")
.description("Produced media type."),
requestMappingConditionField(".produces.[].negated")
.description("Whether the media type is negated."));
List<FieldDescriptor> handlerMethod = Arrays.asList(
fieldWithPath("*.[].details.handlerMethod").optional()
.type(JsonFieldType.OBJECT)
.description("Details of the method, if any, "
+ "that will handle requests to "
+ "this mapping."),
+ "that will handle requests to this mapping."),
fieldWithPath("*.[].details.handlerMethod.className")
.type(JsonFieldType.STRING)
.description("Fully qualified name of the class"
+ " of the method."),
.description("Fully qualified name of the class of the method."),
fieldWithPath("*.[].details.handlerMethod.name")
.type(JsonFieldType.STRING)
.description("Name of the method."),
.type(JsonFieldType.STRING).description("Name of the method."),
fieldWithPath("*.[].details.handlerMethod.descriptor")
.type(JsonFieldType.STRING)
.description("Descriptor of the method as "
+ "specified in the Java Language "
+ "Specification."),
fieldWithPath("*.[].details.handlerFunction")
.optional().type(JsonFieldType.OBJECT)
.description("Details of the function, if any, "
+ "that will handle requests to this "
+ "mapping."),
.description("Descriptor of the method as specified in the Java "
+ "Language Specification."));
List<FieldDescriptor> handlerFunction = Arrays.asList(
fieldWithPath("*.[].details.handlerFunction").optional()
.type(JsonFieldType.OBJECT)
.description("Details of the function, if any, that will handle "
+ "requests to this mapping."),
fieldWithPath("*.[].details.handlerFunction.className")
.type(JsonFieldType.STRING).description(
"Fully qualified name of the class of "
+ "the function."))));
"Fully qualified name of the class of the function."));
List<FieldDescriptor> dispatcherHandlerFields = new ArrayList<>(Arrays.asList(
fieldWithPath("*")
.description("Dispatcher handler mappings, if any, keyed by "
+ "dispatcher handler bean name."),
fieldWithPath("*.[].details").optional().type(JsonFieldType.OBJECT)
.description("Additional implementation-specific "
+ "details about the mapping. Optional."),
fieldWithPath("*.[].handler").description("Handler for the mapping."),
fieldWithPath("*.[].predicate")
.description("Predicate for the mapping.")));
dispatcherHandlerFields.addAll(requestMappingConditions);
dispatcherHandlerFields.addAll(handlerMethod);
dispatcherHandlerFields.addAll(handlerFunction);
this.client.get().uri("/actuator/mappings").exchange().expectStatus().isOk()
.expectBody()
.consumeWith(document("mappings",
responseFields(
beneathPath("contexts.*.mappings.dispatcherHandlers")
.withSubsectionId("dispatcher-handlers"),
dispatcherHandlerFields)));
}
private FieldDescriptor requestMappingConditionField(String path) {
return fieldWithPath("*.[].details.requestMappingConditions" + path);
}
@Configuration
@ -149,6 +195,22 @@ public class MappingsEndpointReactiveDocumentationTests
(request) -> ServerResponse.ok().build());
}
@Bean
public ExampleController exampleController() {
return new ExampleController();
}
}
@RestController
private static class ExampleController {
@PostMapping(path = "/", consumes = { MediaType.APPLICATION_JSON_VALUE,
"!application/xml" }, produces = MediaType.TEXT_PLAIN_VALUE, headers = "X-Custom=Foo", params = "a!=alpha")
public String example() {
return "Hello World";
}
}
}

@ -16,7 +16,10 @@
package org.springframework.boot.actuate.autoconfigure.endpoint.web.documentation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
@ -36,11 +39,15 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.payload.FieldDescriptor;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.restdocs.payload.ResponseFieldsSnippet;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.restdocs.payload.PayloadDocumentation.beneathPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
@ -91,39 +98,75 @@ public class MappingsEndpointServletDocumentationTests
.description("Dispatcher handler mappings, if any.").optional()
.type(JsonFieldType.OBJECT),
parentIdField());
this.client.get().uri("/actuator/mappings").exchange().expectBody()
.consumeWith(document("mappings", commonResponseFields,
responseFields(
beneathPath("contexts.*.mappings.dispatcherServlets")
.withSubsectionId("dispatcher-servlets"),
fieldWithPath("*").description(
"Dispatcher servlet mappings, if any, keyed by "
List<FieldDescriptor> dispatcherServletFields = new ArrayList<>(Arrays.asList(
fieldWithPath("*")
.description("Dispatcher servlet mappings, if any, keyed by "
+ "dispatcher servlet bean name."),
fieldWithPath("*.[].handler")
.description("Handler for the mapping."),
fieldWithPath("*.[].predicate")
.description("Predicate for the mapping."),
fieldWithPath("*.[].details").optional()
.type(JsonFieldType.OBJECT)
fieldWithPath("*.[].details").optional().type(JsonFieldType.OBJECT)
.description("Additional implementation-specific "
+ "details about the mapping. Optional."),
fieldWithPath("*.[].handler").description("Handler for the mapping."),
fieldWithPath("*.[].predicate")
.description("Predicate for the mapping.")));
List<FieldDescriptor> requestMappingConditions = Arrays.asList(
requestMappingConditionField("")
.description("Details of the request mapping conditions.")
.optional(),
requestMappingConditionField(".consumes")
.description("Details of the consumes condition"),
requestMappingConditionField(".consumes.[].mediaType")
.description("Consumed media type."),
requestMappingConditionField(".consumes.[].negated")
.description("Whether the media type is negated."),
requestMappingConditionField(".headers")
.description("Details of the headers condition."),
requestMappingConditionField(".headers.[].name")
.description("Name of the header."),
requestMappingConditionField(".headers.[].value")
.description("Required value of the header, if any."),
requestMappingConditionField(".headers.[].negated")
.description("Whether the value is negated."),
requestMappingConditionField(".methods")
.description("HTTP methods that are handled."),
requestMappingConditionField(".params")
.description("Details of the params condition."),
requestMappingConditionField(".params.[].name")
.description("Name of the parameter."),
requestMappingConditionField(".params.[].value")
.description("Required value of the parameter, if any."),
requestMappingConditionField(".params.[].negated")
.description("Whether the value is negated."),
requestMappingConditionField(".patterns").description(
"Patterns identifying the paths handled by the mapping."),
requestMappingConditionField(".produces")
.description("Details of the produces condition."),
requestMappingConditionField(".produces.[].mediaType")
.description("Produced media type."),
requestMappingConditionField(".produces.[].negated")
.description("Whether the media type is negated."));
List<FieldDescriptor> handlerMethod = Arrays.asList(
fieldWithPath("*.[].details.handlerMethod").optional()
.type(JsonFieldType.OBJECT)
.description("Details of the method, if any, "
+ "that will handle requests to "
+ "this mapping."),
+ "that will handle requests to this mapping."),
fieldWithPath("*.[].details.handlerMethod.className")
.type(JsonFieldType.STRING)
.description("Fully qualified name of the class"
+ " of the method."),
.description("Fully qualified name of the class of the method."),
fieldWithPath("*.[].details.handlerMethod.name")
.type(JsonFieldType.STRING)
.description("Name of the method."),
.type(JsonFieldType.STRING).description("Name of the method."),
fieldWithPath("*.[].details.handlerMethod.descriptor")
.type(JsonFieldType.STRING)
.description("Descriptor of the method as "
+ "specified in the Java Language "
+ "Specification.")),
.description("Descriptor of the method as specified in the Java "
+ "Language Specification."));
dispatcherServletFields.addAll(handlerMethod);
dispatcherServletFields.addAll(requestMappingConditions);
this.client.get().uri("/actuator/mappings").exchange().expectBody()
.consumeWith(document(
"mappings", commonResponseFields,
responseFields(beneathPath(
"contexts.*.mappings.dispatcherServlets")
.withSubsectionId("dispatcher-servlets"),
dispatcherServletFields),
responseFields(
beneathPath("contexts.*.mappings.servletFilters")
.withSubsectionId("servlet-filters"),
@ -146,6 +189,10 @@ public class MappingsEndpointServletDocumentationTests
.description("Class name of the servlet"))));
}
private FieldDescriptor requestMappingConditionField(String path) {
return fieldWithPath("*.[].details.requestMappingConditions" + path);
}
@Configuration
@Import(BaseDocumentationConfiguration.class)
static class TestConfiguration {
@ -177,6 +224,22 @@ public class MappingsEndpointServletDocumentationTests
return new MappingsEndpoint(descriptionProviders, context);
}
@Bean
public ExampleController exampleController() {
return new ExampleController();
}
}
@RestController
private static class ExampleController {
@PostMapping(path = "/", consumes = { MediaType.APPLICATION_JSON_VALUE,
"!application/xml" }, produces = MediaType.TEXT_PLAIN_VALUE, headers = "X-Custom=Foo", params = "a!=alpha")
public String example() {
return "Hello World";
}
}
}

@ -125,6 +125,8 @@ public class DispatcherHandlersMappingDescriptionProvider
DispatcherHandlerMappingDetails handlerMapping = new DispatcherHandlerMappingDetails();
handlerMapping
.setHandlerMethod(new HandlerMethodDescription(mapping.getValue()));
handlerMapping.setRequestMappingConditions(
new RequestMappingConditionsDescription(mapping.getKey()));
return new DispatcherHandlerMappingDescription(mapping.getKey().toString(),
mapping.getValue().toString(), handlerMapping);
}

@ -0,0 +1,142 @@
/*
* Copyright 2012-2018 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.boot.actuate.web.mappings.reactive;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.reactive.result.condition.MediaTypeExpression;
import org.springframework.web.reactive.result.condition.NameValueExpression;
import org.springframework.web.reactive.result.method.RequestMappingInfo;
import org.springframework.web.util.pattern.PathPattern;
/**
* Description of the conditions of a {@link RequestMappingInfo}.
*
* @author Andy Wilkinson
* @since 2.0.0
*/
public class RequestMappingConditionsDescription {
private final List<MediaTypeExpressionDescription> consumes;
private final List<NameValueExpressionDescription> headers;
private final Set<RequestMethod> methods;
private final List<NameValueExpressionDescription> params;
private final Set<String> patterns;
private final List<MediaTypeExpressionDescription> produces;
RequestMappingConditionsDescription(RequestMappingInfo requestMapping) {
this.consumes = requestMapping.getConsumesCondition().getExpressions().stream()
.map(MediaTypeExpressionDescription::new).collect(Collectors.toList());
this.headers = requestMapping.getHeadersCondition().getExpressions().stream()
.map(NameValueExpressionDescription::new).collect(Collectors.toList());
this.methods = requestMapping.getMethodsCondition().getMethods();
this.params = requestMapping.getParamsCondition().getExpressions().stream()
.map(NameValueExpressionDescription::new).collect(Collectors.toList());
this.patterns = requestMapping.getPatternsCondition().getPatterns().stream()
.map(PathPattern::getPatternString).collect(Collectors.toSet());
this.produces = requestMapping.getProducesCondition().getExpressions().stream()
.map(MediaTypeExpressionDescription::new).collect(Collectors.toList());
}
public List<MediaTypeExpressionDescription> getConsumes() {
return this.consumes;
}
public List<NameValueExpressionDescription> getHeaders() {
return this.headers;
}
public Set<RequestMethod> getMethods() {
return this.methods;
}
public List<NameValueExpressionDescription> getParams() {
return this.params;
}
public Set<String> getPatterns() {
return this.patterns;
}
public List<MediaTypeExpressionDescription> getProduces() {
return this.produces;
}
/**
* A description of a {@link MediaTypeExpression} in a request mapping condition.
*/
public static class MediaTypeExpressionDescription {
private final String mediaType;
private final boolean negated;
MediaTypeExpressionDescription(MediaTypeExpression expression) {
this.mediaType = expression.getMediaType().toString();
this.negated = expression.isNegated();
}
public String getMediaType() {
return this.mediaType;
}
public boolean isNegated() {
return this.negated;
}
}
/**
* A description of a {@link NameValueExpression} in a request mapping condition.
*/
public static class NameValueExpressionDescription {
private final String name;
private final Object value;
private final boolean negated;
NameValueExpressionDescription(NameValueExpression<?> expression) {
this.name = expression.getName();
this.value = expression.getValue();
this.negated = expression.isNegated();
}
public String getName() {
return this.name;
}
public Object getValue() {
return this.value;
}
public boolean isNegated() {
return this.negated;
}
}
}

@ -27,14 +27,25 @@ import org.springframework.web.servlet.DispatcherServlet;
*/
public class DispatcherServletMappingDetails {
private final HandlerMethodDescription handlerMethod;
private HandlerMethodDescription handlerMethod;
DispatcherServletMappingDetails(HandlerMethodDescription handlerMethod) {
this.handlerMethod = handlerMethod;
}
private RequestMappingConditionsDescription requestMappingConditions;
public HandlerMethodDescription getHandlerMethod() {
return this.handlerMethod;
}
void setHandlerMethod(HandlerMethodDescription handlerMethod) {
this.handlerMethod = handlerMethod;
}
public RequestMappingConditionsDescription getRequestMappingConditions() {
return this.requestMappingConditions;
}
void setRequestMappingConditions(
RequestMappingConditionsDescription requestMappingConditions) {
this.requestMappingConditions = requestMappingConditions;
}
}

@ -140,9 +140,13 @@ public class DispatcherServletsMappingDescriptionProvider
private DispatcherServletMappingDescription describe(
Entry<RequestMappingInfo, HandlerMethod> mapping) {
DispatcherServletMappingDetails mappingDetails = new DispatcherServletMappingDetails();
mappingDetails
.setHandlerMethod(new HandlerMethodDescription(mapping.getValue()));
mappingDetails.setRequestMappingConditions(
new RequestMappingConditionsDescription(mapping.getKey()));
return new DispatcherServletMappingDescription(mapping.getKey().toString(),
mapping.getValue().toString(), new DispatcherServletMappingDetails(
new HandlerMethodDescription(mapping.getValue())));
mapping.getValue().toString(), mappingDetails);
}
}

@ -0,0 +1,140 @@
/*
* Copyright 2012-2018 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.boot.actuate.web.mappings.servlet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.condition.MediaTypeExpression;
import org.springframework.web.servlet.mvc.condition.NameValueExpression;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
/**
* Description of the conditions of a {@link RequestMappingInfo}.
*
* @author Andy Wilkinson
* @since 2.0.0
*/
public class RequestMappingConditionsDescription {
private final List<MediaTypeExpressionDescription> consumes;
private final List<NameValueExpressionDescription> headers;
private final Set<RequestMethod> methods;
private final List<NameValueExpressionDescription> params;
private final Set<String> patterns;
private final List<MediaTypeExpressionDescription> produces;
RequestMappingConditionsDescription(RequestMappingInfo requestMapping) {
this.consumes = requestMapping.getConsumesCondition().getExpressions().stream()
.map(MediaTypeExpressionDescription::new).collect(Collectors.toList());
this.headers = requestMapping.getHeadersCondition().getExpressions().stream()
.map(NameValueExpressionDescription::new).collect(Collectors.toList());
this.methods = requestMapping.getMethodsCondition().getMethods();
this.params = requestMapping.getParamsCondition().getExpressions().stream()
.map(NameValueExpressionDescription::new).collect(Collectors.toList());
this.patterns = requestMapping.getPatternsCondition().getPatterns();
this.produces = requestMapping.getProducesCondition().getExpressions().stream()
.map(MediaTypeExpressionDescription::new).collect(Collectors.toList());
}
public List<MediaTypeExpressionDescription> getConsumes() {
return this.consumes;
}
public List<NameValueExpressionDescription> getHeaders() {
return this.headers;
}
public Set<RequestMethod> getMethods() {
return this.methods;
}
public List<NameValueExpressionDescription> getParams() {
return this.params;
}
public Set<String> getPatterns() {
return this.patterns;
}
public List<MediaTypeExpressionDescription> getProduces() {
return this.produces;
}
/**
* A description of a {@link MediaTypeExpression} in a request mapping condition.
*/
public static class MediaTypeExpressionDescription {
private final String mediaType;
private final boolean negated;
MediaTypeExpressionDescription(MediaTypeExpression expression) {
this.mediaType = expression.getMediaType().toString();
this.negated = expression.isNegated();
}
public String getMediaType() {
return this.mediaType;
}
public boolean isNegated() {
return this.negated;
}
}
/**
* A description of a {@link NameValueExpression} in a request mapping condition.
*/
public static class NameValueExpressionDescription {
private final String name;
private final Object value;
private final boolean negated;
NameValueExpressionDescription(NameValueExpression<?> expression) {
this.name = expression.getName();
this.value = expression.getValue();
this.negated = expression.isNegated();
}
public String getName() {
return this.name;
}
public Object getValue() {
return this.value;
}
public boolean isNegated() {
return this.negated;
}
}
}

@ -0,0 +1,16 @@
package sample.webflux;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ExampleController {
@PostMapping(path = "/", consumes = { MediaType.APPLICATION_JSON_VALUE,
"!application/xml" }, produces = MediaType.TEXT_PLAIN_VALUE, headers = "X-Custom=Foo", params = "a!=alpha")
public String example() {
return "Hello World";
}
}
Loading…
Cancel
Save