Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
5fcf45c4
Commit
5fcf45c4
authored
Jul 10, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.0.x'
parents
a5b3d549
3423c5dd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
122 additions
and
28 deletions
+122
-28
DispatcherServletsMappingDescriptionProvider.java
...servlet/DispatcherServletsMappingDescriptionProvider.java
+29
-4
MappingsEndpointTests.java
...work/boot/actuate/web/mappings/MappingsEndpointTests.java
+84
-16
ServletRegistrationBean.java
...ngframework/boot/web/servlet/ServletRegistrationBean.java
+9
-8
No files found.
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/mappings/servlet/DispatcherServletsMappingDescriptionProvider.java
View file @
5fcf45c4
...
...
@@ -19,14 +19,18 @@ package org.springframework.boot.actuate.web.mappings.servlet;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
import
javax.servlet.Servlet
;
import
org.springframework.boot.actuate.web.mappings.HandlerMethodDescription
;
import
org.springframework.boot.actuate.web.mappings.MappingDescriptionProvider
;
import
org.springframework.boot.web.servlet.ServletRegistrationBean
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.data.rest.webmvc.support.DelegatingHandlerMapping
;
import
org.springframework.util.ClassUtils
;
...
...
@@ -44,6 +48,7 @@ import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMappi
* DispatcherServlets}.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @since 2.0.0
*/
public
class
DispatcherServletsMappingDescriptionProvider
...
...
@@ -81,13 +86,33 @@ public class DispatcherServletsMappingDescriptionProvider
private
Map
<
String
,
List
<
DispatcherServletMappingDescription
>>
describeMappings
(
WebApplicationContext
context
)
{
Map
<
String
,
List
<
DispatcherServletMappingDescription
>>
mappings
=
new
HashMap
<>();
context
.
getBeansOfType
(
DispatcherServlet
.
class
)
.
forEach
((
name
,
dispatcherServlet
)
->
mappings
.
put
(
name
,
describeMappings
(
new
DispatcherServletHandlerMappings
(
name
,
dispatcherServlet
,
context
))));
determineDispatcherServlets
(
context
).
forEach
((
name
,
dispatcherServlet
)
->
mappings
.
put
(
name
,
describeMappings
(
new
DispatcherServletHandlerMappings
(
name
,
dispatcherServlet
,
context
))));
return
mappings
;
}
private
Map
<
String
,
DispatcherServlet
>
determineDispatcherServlets
(
WebApplicationContext
context
)
{
Map
<
String
,
DispatcherServlet
>
dispatcherServlets
=
new
LinkedHashMap
<>();
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
).
values
()
.
forEach
((
registration
)
->
{
Servlet
servlet
=
registration
.
getServlet
();
if
(
servlet
instanceof
DispatcherServlet
&&
!
dispatcherServlets
.
containsValue
(
servlet
))
{
dispatcherServlets
.
put
(
registration
.
getServletName
(),
(
DispatcherServlet
)
servlet
);
}
});
context
.
getBeansOfType
(
DispatcherServlet
.
class
)
.
forEach
((
name
,
dispatcherServlet
)
->
{
if
(!
dispatcherServlets
.
containsValue
(
dispatcherServlet
))
{
dispatcherServlets
.
put
(
name
,
dispatcherServlet
);
}
});
return
dispatcherServlets
;
}
private
List
<
DispatcherServletMappingDescription
>
describeMappings
(
DispatcherServletHandlerMappings
mappings
)
{
return
mappings
.
getHandlerMappings
().
stream
().
flatMap
(
this
::
describe
)
...
...
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/mappings/MappingsEndpointTests.java
View file @
5fcf45c4
...
...
@@ -41,6 +41,7 @@ import org.springframework.boot.actuate.web.mappings.servlet.ServletRegistration
import
org.springframework.boot.actuate.web.mappings.servlet.ServletsMappingDescriptionProvider
;
import
org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner
;
import
org.springframework.boot.test.context.runner.WebApplicationContextRunner
;
import
org.springframework.boot.web.servlet.ServletRegistrationBean
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -67,28 +68,14 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.r
* Tests for {@link MappingsEndpoint}.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
public
class
MappingsEndpointTests
{
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
servletWebMappings
()
{
ServletContext
servletContext
=
mock
(
ServletContext
.
class
);
given
(
servletContext
.
getInitParameterNames
())
.
willReturn
(
Collections
.
emptyEnumeration
());
given
(
servletContext
.
getAttributeNames
())
.
willReturn
(
Collections
.
emptyEnumeration
());
FilterRegistration
filterRegistration
=
mock
(
FilterRegistration
.
class
);
given
((
Map
<
String
,
FilterRegistration
>)
servletContext
.
getFilterRegistrations
())
.
willReturn
(
Collections
.
singletonMap
(
"testFilter"
,
filterRegistration
));
ServletRegistration
servletRegistration
=
mock
(
ServletRegistration
.
class
);
given
((
Map
<
String
,
ServletRegistration
>)
servletContext
.
getServletRegistrations
())
.
willReturn
(
Collections
.
singletonMap
(
"testServlet"
,
servletRegistration
));
Supplier
<
ConfigurableWebApplicationContext
>
contextSupplier
=
()
->
{
AnnotationConfigWebApplicationContext
context
=
new
AnnotationConfigWebApplicationContext
();
context
.
setServletContext
(
servletContext
);
return
context
;
};
Supplier
<
ConfigurableWebApplicationContext
>
contextSupplier
=
prepareContextSupplier
();
new
WebApplicationContextRunner
(
contextSupplier
)
.
withUserConfiguration
(
EndpointConfiguration
.
class
,
ServletWebConfiguration
.
class
)
...
...
@@ -112,6 +99,47 @@ public class MappingsEndpointTests {
});
}
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
servletWebMappingsWithAdditionalDispatcherServlets
()
{
Supplier
<
ConfigurableWebApplicationContext
>
contextSupplier
=
prepareContextSupplier
();
new
WebApplicationContextRunner
(
contextSupplier
).
withUserConfiguration
(
EndpointConfiguration
.
class
,
ServletWebConfiguration
.
class
,
CustomDispatcherServletConfiguration
.
class
).
run
((
context
)
->
{
ContextMappings
contextMappings
=
contextMappings
(
context
);
Map
<
String
,
List
<
DispatcherServletMappingDescription
>>
dispatcherServlets
=
mappings
(
contextMappings
,
"dispatcherServlets"
);
assertThat
(
dispatcherServlets
).
containsOnlyKeys
(
"dispatcherServlet"
,
"customDispatcherServletRegistration"
,
"anotherDispatcherServletRegistration"
);
assertThat
(
dispatcherServlets
.
get
(
"dispatcherServlet"
)).
hasSize
(
1
);
assertThat
(
dispatcherServlets
.
get
(
"customDispatcherServletRegistration"
))
.
hasSize
(
1
);
assertThat
(
dispatcherServlets
.
get
(
"anotherDispatcherServletRegistration"
)).
hasSize
(
1
);
});
}
private
Supplier
<
ConfigurableWebApplicationContext
>
prepareContextSupplier
()
{
ServletContext
servletContext
=
mock
(
ServletContext
.
class
);
given
(
servletContext
.
getInitParameterNames
())
.
willReturn
(
Collections
.
emptyEnumeration
());
given
(
servletContext
.
getAttributeNames
())
.
willReturn
(
Collections
.
emptyEnumeration
());
FilterRegistration
filterRegistration
=
mock
(
FilterRegistration
.
class
);
given
((
Map
<
String
,
FilterRegistration
>)
servletContext
.
getFilterRegistrations
())
.
willReturn
(
Collections
.
singletonMap
(
"testFilter"
,
filterRegistration
));
ServletRegistration
servletRegistration
=
mock
(
ServletRegistration
.
class
);
given
((
Map
<
String
,
ServletRegistration
>)
servletContext
.
getServletRegistrations
())
.
willReturn
(
Collections
.
singletonMap
(
"testServlet"
,
servletRegistration
));
return
()
->
{
AnnotationConfigWebApplicationContext
context
=
new
AnnotationConfigWebApplicationContext
();
context
.
setServletContext
(
servletContext
);
return
context
;
};
}
@Test
public
void
reactiveWebMappings
()
{
new
ReactiveWebApplicationContextRunner
()
...
...
@@ -213,4 +241,44 @@ public class MappingsEndpointTests {
}
@Configuration
static
class
CustomDispatcherServletConfiguration
{
@Bean
public
ServletRegistrationBean
<
DispatcherServlet
>
customDispatcherServletRegistration
(
WebApplicationContext
context
)
{
ServletRegistrationBean
<
DispatcherServlet
>
registration
=
new
ServletRegistrationBean
<>(
createTestDispatcherServlet
(
context
));
registration
.
setName
(
"customDispatcherServletRegistration"
);
return
registration
;
}
@Bean
public
DispatcherServlet
anotherDispatcherServlet
(
WebApplicationContext
context
)
{
return
createTestDispatcherServlet
(
context
);
}
@Bean
public
ServletRegistrationBean
<
DispatcherServlet
>
anotherDispatcherServletRegistration
(
WebApplicationContext
context
)
{
ServletRegistrationBean
<
DispatcherServlet
>
registrationBean
=
new
ServletRegistrationBean
<>(
anotherDispatcherServlet
(
context
));
registrationBean
.
setName
(
"anotherDispatcherServletRegistration"
);
return
registrationBean
;
}
private
DispatcherServlet
createTestDispatcherServlet
(
WebApplicationContext
context
)
{
try
{
DispatcherServlet
dispatcherServlet
=
new
DispatcherServlet
(
context
);
dispatcherServlet
.
init
(
new
MockServletConfig
());
return
dispatcherServlet
;
}
catch
(
ServletException
ex
)
{
throw
new
IllegalStateException
(
ex
);
}
}
}
}
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletRegistrationBean.java
View file @
5fcf45c4
...
...
@@ -100,14 +100,6 @@ public class ServletRegistrationBean<T extends Servlet>
this
.
urlMappings
.
addAll
(
Arrays
.
asList
(
urlMappings
));
}
/**
* Returns the servlet being registered.
* @return the servlet
*/
protected
T
getServlet
()
{
return
this
.
servlet
;
}
/**
* Sets the servlet to be registered.
* @param servlet the servlet
...
...
@@ -117,6 +109,15 @@ public class ServletRegistrationBean<T extends Servlet>
this
.
servlet
=
servlet
;
}
/**
* Return the servlet being registered.
* @return the servlet
* @since 2.0.4
*/
public
T
getServlet
()
{
return
this
.
servlet
;
}
/**
* Set the URL mappings for the servlet. If not specified the mapping will default to
* '/'. This will replace any previously specified mappings.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment