Skip to content

io microsphere spring webmvc util SpringWebMvcHelper

github-actions[bot] edited this page Jun 18, 2026 · 18 revisions

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

Overview

SpringWebHelper for Spring WebMVC

Declaration

public class SpringWebMvcHelper implements SpringWebHelper

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.27-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

getMethod

SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(httpServletRequest, httpServletResponse);
  String method = helper.getMethod(webRequest); // e.g. "GET"

setHeader

SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  helper.setHeader(webRequest, "Content-Type", "application/json");

addHeader

SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  helper.addHeader(webRequest, "Accept", "text/html", "application/json");

getCookieValue

SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  String sessionId = helper.getCookieValue(webRequest, "JSESSIONID");

addCookie

SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  helper.addCookie(webRequest, "session", "abc123");

getBestMatchingHandler

SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  Object handler = helper.getBestMatchingHandler(webRequest);

getPathWithinHandlerMapping

SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  String path = helper.getPathWithinHandlerMapping(webRequest); // e.g. "/users/123"

getBestMatchingPattern

SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  String pattern = helper.getBestMatchingPattern(webRequest); // e.g. "/users/{id}"

getProducibleMediaTypes

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);

getType

SpringWebMvcHelper helper = new SpringWebMvcHelper();
  SpringWebType type = helper.getType(); // returns SpringWebType.WEB_MVC

Usage

Maven Dependency

Add 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

import io.microsphere.spring.webmvc.util.SpringWebMvcHelper;

API Reference

Public Methods

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.

Method Details

getMethod

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.

Example Usage

`SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(httpServletRequest, httpServletResponse);
  String method = helper.getMethod(webRequest); // e.g. "GET"
`

setHeader

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.

Example Usage

`SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  helper.setHeader(webRequest, "Content-Type", "application/json");
`

addHeader

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.

Example Usage

`SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  helper.addHeader(webRequest, "Accept", "text/html", "application/json");
`

getCookieValue

public String getCookieValue(NativeWebRequest request, String cookieName)

Gets the value of a cookie with the specified name from the underlying HttpServletRequest.

Example Usage

`SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  String sessionId = helper.getCookieValue(webRequest, "JSESSIONID");
`

addCookie

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.

Example Usage

`SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  helper.addCookie(webRequest, "session", "abc123");
`

getBestMatchingHandler

public Object getBestMatchingHandler(NativeWebRequest request)

Gets the best matching handler from the request attributes, as resolved by Spring MVC's HandlerMapping.

Example Usage

`SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  Object handler = helper.getBestMatchingHandler(webRequest);
`

getPathWithinHandlerMapping

public String getPathWithinHandlerMapping(NativeWebRequest request)

Gets the path within the handler mapping from the request attributes.

Example Usage

`SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  String path = helper.getPathWithinHandlerMapping(webRequest); // e.g. "/users/123"
`

getBestMatchingPattern

public String getBestMatchingPattern(NativeWebRequest request)

Gets the best matching URL pattern from the request attributes, as resolved by Spring MVC's HandlerMapping.

Example Usage

`SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  String pattern = helper.getBestMatchingPattern(webRequest); // e.g. "/users/{id`"
}

getProducibleMediaTypes

public Set<MediaType> getProducibleMediaTypes(NativeWebRequest request)

Gets the URI template variables from the request attributes, as resolved by Spring MVC's HandlerMapping.

Example Usage

`SpringWebMvcHelper helper = new SpringWebMvcHelper();
  ServletWebRequest webRequest = new ServletWebRequest(request, response);
  // For a request matching "/users/{id`", returns {"id": "123"}
  Map vars = helper.getUriTemplateVariables(webRequest);
}

getType

public SpringWebType getType()

Returns the SpringWebType indicating this helper is for Spring Web MVC.

Example Usage

`SpringWebMvcHelper helper = new SpringWebMvcHelper();
  SpringWebType type = helper.getType(); // returns SpringWebType.WEB_MVC
`

See Also

  • SpringWebHelper

This documentation was auto-generated from the source code of microsphere-spring.

Home

spring-context

spring-guice

spring-jdbc

spring-test

spring-web

spring-webflux

spring-webmvc

Clone this wiki locally