-
Notifications
You must be signed in to change notification settings - Fork 39
io microsphere spring webmvc util SpringWebMvcHelper
Type: Class | Module: microsphere-spring-webmvc | Package: io.microsphere.spring.webmvc.util | Since: 1.0.0
Source:
microsphere-spring-webmvc/src/main/java/io/microsphere/spring/webmvc/util/SpringWebMvcHelper.java
SpringWebHelper for Spring WebMVC
public class SpringWebMvcHelper implements SpringWebHelperAuthor: Mercy
-
Introduced in:
1.0.0 -
Current Project Version:
0.2.27-SNAPSHOT
This component is tested and compatible with the following Java versions:
| Java Version | Status |
|---|---|
| Java 17 | ✅ Compatible |
| Java 21 | ✅ Compatible |
| Java 25 | ✅ Compatible |
SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(httpServletRequest, httpServletResponse);
String method = helper.getMethod(webRequest); // e.g. "GET"SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
helper.setHeader(webRequest, "Content-Type", "application/json");SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
helper.addHeader(webRequest, "Accept", "text/html", "application/json");SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
String sessionId = helper.getCookieValue(webRequest, "JSESSIONID");SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
helper.addCookie(webRequest, "session", "abc123");SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
Object handler = helper.getBestMatchingHandler(webRequest);SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
String path = helper.getPathWithinHandlerMapping(webRequest); // e.g. "/users/123"SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
String pattern = helper.getBestMatchingPattern(webRequest); // e.g. "/users/{id}"SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
// For a request matching "/users/{id}", returns {"id": "123"}
Map<String, String> vars = helper.getUriTemplateVariables(webRequest);SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
// For a request like "/users;color=red;size=10"
Map<String, MultiValueMap<String, String>> matrix = helper.getMatrixVariables(webRequest);SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
Set<MediaType> mediaTypes = helper.getProducibleMediaTypes(webRequest);SpringWebMvcHelper helper = new SpringWebMvcHelper();
SpringWebType type = helper.getType(); // returns SpringWebType.WEB_MVCAdd the following dependency to your pom.xml:
<dependency>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-spring-webmvc</artifactId>
<version>${microsphere-spring.version}</version>
</dependency>Tip: Use the BOM (
microsphere-spring-dependencies) for consistent version management. See the Getting Started guide.
import io.microsphere.spring.webmvc.util.SpringWebMvcHelper;| Method | Description |
|---|---|
getMethod |
Gets the HTTP method from the given NativeWebRequest. First checks for a method |
setHeader |
Sets a response header on the underlying HttpServletResponse. |
addHeader |
Adds one or more values to a response header on the underlying HttpServletResponse. |
getCookieValue |
Gets the value of a cookie with the specified name from the underlying HttpServletRequest. |
addCookie |
Adds a cookie with the specified name and value to the underlying HttpServletResponse. |
getBestMatchingHandler |
Gets the best matching handler from the request attributes, as resolved by Spring MVC's |
getPathWithinHandlerMapping |
Gets the path within the handler mapping from the request attributes. |
getBestMatchingPattern |
Gets the best matching URL pattern from the request attributes, as resolved by Spring MVC's |
getProducibleMediaTypes |
Gets the URI template variables from the request attributes, as resolved by Spring MVC's |
getType |
Returns the SpringWebType indicating this helper is for Spring Web MVC. |
public String getMethod(NativeWebRequest request)Gets the HTTP method from the given NativeWebRequest. First checks for a method
override header, then falls back to the actual servlet request method.
`SpringWebMvcHelper helper = new SpringWebMvcHelper(); ServletWebRequest webRequest = new ServletWebRequest(httpServletRequest, httpServletResponse); String method = helper.getMethod(webRequest); // e.g. "GET" `
public void setHeader(NativeWebRequest request, String headerName, String headerValue)Sets a response header on the underlying HttpServletResponse.
If the header already exists, its value is replaced.
`SpringWebMvcHelper helper = new SpringWebMvcHelper(); ServletWebRequest webRequest = new ServletWebRequest(request, response); helper.setHeader(webRequest, "Content-Type", "application/json"); `
public void addHeader(NativeWebRequest request, String headerName, String... headerValues)Adds one or more values to a response header on the underlying HttpServletResponse.
Unlike #setHeader, this appends values rather than replacing existing ones.
`SpringWebMvcHelper helper = new SpringWebMvcHelper(); ServletWebRequest webRequest = new ServletWebRequest(request, response); helper.addHeader(webRequest, "Accept", "text/html", "application/json"); `
public String getCookieValue(NativeWebRequest request, String cookieName)Gets the value of a cookie with the specified name from the underlying HttpServletRequest.
`SpringWebMvcHelper helper = new SpringWebMvcHelper(); ServletWebRequest webRequest = new ServletWebRequest(request, response); String sessionId = helper.getCookieValue(webRequest, "JSESSIONID"); `
public void addCookie(NativeWebRequest request, String cookieName, String cookieValue)Adds a cookie with the specified name and value to the underlying HttpServletResponse.
The cookie is created with secure and httpOnly flags enabled.
`SpringWebMvcHelper helper = new SpringWebMvcHelper(); ServletWebRequest webRequest = new ServletWebRequest(request, response); helper.addCookie(webRequest, "session", "abc123"); `
public Object getBestMatchingHandler(NativeWebRequest request)Gets the best matching handler from the request attributes, as resolved by Spring MVC's
HandlerMapping.
`SpringWebMvcHelper helper = new SpringWebMvcHelper(); ServletWebRequest webRequest = new ServletWebRequest(request, response); Object handler = helper.getBestMatchingHandler(webRequest); `
public String getPathWithinHandlerMapping(NativeWebRequest request)Gets the path within the handler mapping from the request attributes.
`SpringWebMvcHelper helper = new SpringWebMvcHelper(); ServletWebRequest webRequest = new ServletWebRequest(request, response); String path = helper.getPathWithinHandlerMapping(webRequest); // e.g. "/users/123" `
public String getBestMatchingPattern(NativeWebRequest request)Gets the best matching URL pattern from the request attributes, as resolved by Spring MVC's
HandlerMapping.
`SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
String pattern = helper.getBestMatchingPattern(webRequest); // e.g. "/users/{id`"
}
public Set<MediaType> getProducibleMediaTypes(NativeWebRequest request)Gets the URI template variables from the request attributes, as resolved by Spring MVC's
HandlerMapping.
`SpringWebMvcHelper helper = new SpringWebMvcHelper();
ServletWebRequest webRequest = new ServletWebRequest(request, response);
// For a request matching "/users/{id`", returns {"id": "123"}
Map vars = helper.getUriTemplateVariables(webRequest);
}
public SpringWebType getType()Returns the SpringWebType indicating this helper is for Spring Web MVC.
`SpringWebMvcHelper helper = new SpringWebMvcHelper(); SpringWebType type = helper.getType(); // returns SpringWebType.WEB_MVC `
SpringWebHelper
This documentation was auto-generated from the source code of microsphere-spring.
spring-context
- AbstractInjectionPointDependencyResolver
- AbstractSmartLifecycle
- AbstractSpringResourceURLConnection
- AnnotatedBeanCapableImportBeanDefinitionRegistrar
- AnnotatedBeanCapableImportCandidate
- AnnotatedBeanCapableImportSelector
- AnnotatedBeanDefinitionRegistryUtils
- AnnotatedInjectionBeanPostProcessor
- AnnotatedInjectionPointDependencyResolver
- AnnotatedPropertySourceLoader
- AnnotationBeanDefinitionRegistryPostProcessor
- AnnotationUtils
- ApplicationContextUtils
- ApplicationEventInterceptor
- ApplicationEventInterceptorChain
- ApplicationListenerInterceptor
- ApplicationListenerInterceptorChain
- AutoRegistrationBean
- AutoRegistrationBeanRegistrar
- AutowireCandidateResolvingListener
- AutowiredInjectionPointDependencyResolver
- BeanCapableImportCandidate
- BeanDefinitionUtils
- BeanDependencyResolver
- BeanFactoryListener
- BeanFactoryListenerAdapter
- BeanFactoryListeners
- BeanFactoryUtils
- BeanListener
- BeanListenerAdapter
- BeanListeners
- BeanMethodInjectionPointDependencyResolver
- BeanPropertyChangedEvent
- BeanRegistrar
- BeanSource
- BeanTimeStatistics
- BeanUtils
- CollectingConfigurationPropertyListener
- CompositeAutowireCandidateResolvingListener
- ConfigurationBeanAliasGenerator
- ConfigurationBeanBinder
- ConfigurationBeanBindingPostProcessor
- ConfigurationBeanBindingRegistrar
- ConfigurationBeanBindingsRegister
- ConfigurationBeanCustomizer
- ConfigurationPropertyOverrideAnnotationAttributesStrategy
- ConfigurationPropertyRepository
- ConstructionInjectionPointDependencyResolver
- ConversionServiceResolver
- ConversionServiceUtils
- DefaultApplicationEventInterceptorChain
- DefaultApplicationListenerInterceptorChain
- DefaultBeanDependencyResolver
- DefaultConfigurationBeanAliasGenerator
- DefaultConfigurationBeanBinder
- DefaultPropertiesPropertySource
- DefaultPropertiesPropertySourceLoader
- DefaultPropertiesPropertySources
- DefaultPropertiesPropertySourcesLoader
- DefaultResourceComparator
- DelegatingFactoryBean
- Dependency
- DependencyAnalysisBeanFactoryListener
- DependencyTreeWalker
- EnableAutoRegistrationBean
- EnableConfigurationBeanBinding
- EnableConfigurationBeanBindings
- EnableEventExtension
- EnableSpringConverterAdapter
- EnableSpringConverterAdapterRegistrar
- EnableTTLCaching
- EnvironmentListener
- EnvironmentUtils
- EventExtensionAttributes
- EventExtensionRegistrar
- EventPublishingBeanAfterProcessor
- EventPublishingBeanBeforeProcessor
- EventPublishingBeanInitializer
- ExposingClassPathBeanDefinitionScanner
- FilterMode
- GenericAnnotationAttributes
- GenericApplicationListenerAdapter
- GenericBeanNameGenerator
- GenericBeanPostProcessorAdapter
- HyphenAliasGenerator
- ImmutableMapPropertySource
- ImportOptional
- ImportOptionalSelector
- InjectionPointDependencyResolver
- InjectionPointDependencyResolvers
- InterceptingApplicationEventMulticaster
- InterceptingApplicationEventMulticasterProxy
- InterceptingApplicationListener
- JavaBeansPropertyChangeListenerAdapter
- JoinAliasGenerator
- JsonPropertySource
- JsonPropertySourceFactory
- ListenableAutowireCandidateResolver
- ListenableAutowireCandidateResolverInitializer
- ListenableConfigurableEnvironment
- ListenableConfigurableEnvironmentInitializer
- LoggingAutowireCandidateResolvingListener
- LoggingBeanFactoryListener
- LoggingBeanListener
- LoggingEnvironmentListener
- LoggingSmartLifecycle
- MethodParameterUtils
- MimeTypeUtils
- NamedBeanHolderComparator
- OnceApplicationContextEventListener
- OverrideAnnotationAttributes
- OverrideAnnotationAttributesStrategy
- ParallelPreInstantiationSingletonsBeanFactoryListener
- ProfileListener
- PropertiesUtils
- PropertyConstants
- PropertyResolverListener
- PropertyResolverUtils
- PropertySourceChangedEvent
- PropertySourceExtension
- PropertySourceExtensionAttributes
- PropertySourceExtensionLoader
- PropertySourcesChangedEvent
- PropertySourcesUtils
- PropertyValuesUtils
- ResolvableDependencyTypeFilter
- ResolvablePlaceholderAnnotationAttributes
- ResourceInjectionPointDependencyResolver
- ResourceLoaderUtils
- ResourcePropertySource
- ResourcePropertySourceLoader
- ResourcePropertySources
- ResourcePropertySourcesLoader
- ResourceUtils
- ResourceYamlProcessor
- SpringConverterAdapter
- SpringDelegatingBeanProtocolURLConnectionFactory
- SpringEnvironmentURLConnectionFactory
- SpringFactoriesLoaderUtils
- SpringProfilesURLConnectionAdapter
- SpringPropertySourcesURLConnectionAdapter
- SpringProtocolURLStreamHandler
- SpringResourceURLConnection
- SpringResourceURLConnectionAdapter
- SpringResourceURLConnectionFactory
- SpringSubProtocolURLConnectionFactory
- SpringVersion
- SpringVersionUtils
- TTLCachePut
- TTLCacheResolver
- TTLCacheable
- TTLCachingConfiguration
- TTLContext
- UnderScoreJoinAliasGenerator
- YamlPropertySource
- YamlPropertySourceFactory
spring-guice
spring-jdbc
- CompoundJdbcEventListenerFactory
- EnableP6DataSource
- NoOpP6LoadableOptions
- P6DataSourceBeanDefinitionRegistrar
- P6DataSourceBeanPostProcessor
- PropertySourcesP6LoadableOptionsAdapter
- SpringP6SpyURLConnectionFactory
spring-test
- AbstractWebFluxTest
- AbstractWebMvcTest
- AnnotatedTypeMetadataTestFactory
- EmbeddedDataBaseBeanDefinitionRegistrar
- EmbeddedDataBaseBeanDefinitionsRegistrar
- EmbeddedDatabaseType
- EmbeddedTomcatConfiguration
- EmbeddedTomcatContextLoader
- EmbeddedTomcatMergedContextConfiguration
- EmbeddedTomcatTestContextBootstrapper
- EmbeddedZookeeperServer
- EmbeddedZookeeperServerTestExecutionListener
- EnableEmbeddedDatabase
- EnableEmbeddedDatabases
- MockServletWebRequest
- PersonHandler
- PersonHandler
- RouterFunctionTestConfig
- RouterFunctionTestConfig
- ServletTestUtils
- SimpleUrlHandlerMappingTestConfig
- SimpleUrlHandlerMappingTestConfig
- SpringLoggingTest
- SpringTestUtils
- SpringTestWebUtils
- TestConditionContext
- TestController
- TestFilter
- TestFilterRegistration
- TestServlet
- TestServletContext
- TestServletContextListener
- TestServletRegistration
- User
- WebTestUtils
spring-web
- AbstractNameValueExpression
- AbstractWebEndpointMappingFactory
- AbstractWebRequestRule
- CompositeWebEndpointMappingRegistry
- CompositeWebRequestRule
- ConsumeMediaTypeExpression
- DelegatingHandlerMethodAdvice
- EnableWebExtension
- FilterRegistrationWebEndpointMappingFactory
- FilteringWebEndpointMappingRegistry
- GenericMediaTypeExpression
- HandlerMetadata
- HandlerMethodAdvice
- HandlerMethodArgumentInterceptor
- HandlerMethodArgumentsResolvedEvent
- HandlerMethodInterceptor
- HandlerMethodMetadata
- HttpUtils
- Jackson2WebEndpointMappingFactory
- MediaTypeExpression
- MediaTypeUtils
- NameValueExpression
- ProduceMediaTypeExpression
- PropertyConstants
- RegistrationWebEndpointMappingFactory
- RequestAttributesUtils
- RequestContextStrategy
- ServletRegistrationWebEndpointMappingFactory
- ServletWebEndpointMappingResolver
- SimpleWebEndpointMappingRegistry
- SmartWebEndpointMappingFactory
- SpringWebHelper
- SpringWebType
- UnknownSpringWebHelper
- WebEndpointMapping
- WebEndpointMappingFactory
- WebEndpointMappingFilter
- WebEndpointMappingRegistrar
- WebEndpointMappingRegistry
- WebEndpointMappingResolver
- WebEndpointMappingsReadyEvent
- WebEventPublisher
- WebExtensionBeanDefinitionRegistrar
- WebRequestConsumesRule
- WebRequestHeaderExpression
- WebRequestHeadersRule
- WebRequestMethodsRule
- WebRequestParamExpression
- WebRequestParamsRule
- WebRequestPattensRule
- WebRequestProducesRule
- WebRequestRule
- WebRequestUtils
- WebScope
- WebSource
- WebTarget
- WebType
- WebUtils
spring-webflux
- CompositeWebFilter
- ConsumingWebEndpointMappingAdapter
- DelegatingWebFilter
- EnableWebFluxExtension
- HandlerMappingWebEndpointMappingFactory
- HandlerMappingWebEndpointMappingResolver
- HandlerMetadataWebEndpointMappingFactory
- InterceptingHandlerMethodProcessor
- MonoUtils
- RequestContextWebFilter
- RequestHandledEventPublishingWebFilter
- RequestMappingMetadataWebEndpointMappingFactory
- RequestPredicateKind
- RequestPredicateVisitorAdapter
- ReversedProxyHandlerMapping
- RouterFunctionVisitorAdapter
- ServerRequestHandledEvent
- ServerWebRequest
- SpringWebFluxHelper
- StoringRequestBodyArgumentInterceptor
- StoringResponseBodyReturnValueInterceptor
- WebFluxExtensionBeanDefinitionRegistrar
- WebServerScope
- WebServerUtils
spring-webmvc
- AbstractPageRenderContextHandlerInterceptor
- AnnotatedMethodHandlerInterceptor
- ConfigurableContentNegotiationManagerWebMvcConfigurer
- ConsumingWebEndpointMappingAdapter
- ContentCachingFilter
- EnableWebMvcExtension
- EnableWebMvcExtensionListener
- ExclusiveViewResolverApplicationListener
- HandlerMappingWebEndpointMappingFactory
- HandlerMappingWebEndpointMappingResolver
- HandlerMetadataWebEndpointMappingFactory
- HandlerMethodArgumentResolverAdvice
- InterceptingHandlerMethodProcessor
- LazyCompositeHandlerInterceptor
- LoggingHandlerMethodArgumentResolverAdvice
- LoggingMethodHandlerInterceptor
- LoggingPageRenderContextHandlerInterceptor
- MethodHandlerInterceptor
- PropertyConstants
- RequestBodyAdviceAdapter
- RequestMappingMetadata
- RequestMappingMetadataWebEndpointMappingFactory
- RequestPredicateVisitorAdapter
- ResponseBodyAdviceAdapter
- ReversedProxyHandlerMapping
- RouterFunctionVisitorAdapter
- SpringWebMvcHelper
- StoringRequestBodyArgumentAdvice
- StoringResponseBodyReturnValueAdvice
- ViewResolverUtils
- ViewUtils
- WebMvcExtensionBeanDefinitionRegistrar
- WebMvcExtensionConfiguration
- WebMvcUtils
- WebUtils