org.springframework.web.accept

## Class PathExtensionContentNegotiationStrategy

- java.lang.Object
- - org.springframework.web.accept.MappingMediaTypeFileExtensionResolver
  - - org.springframework.web.accept.AbstractMappingContentNegotiationStrategy
    - - org.springframework.web.accept.PathExtensionContentNegotiationStrategy

- All Implemented Interfaces:
    ContentNegotiationStrategy, MediaTypeFileExtensionResolver
  Direct Known Subclasses:
    ServletPathExtensionContentNegotiationStrategy

      public class PathExtensionContentNegotiationStrategy
      extends AbstractMappingContentNegotiationStrategy

  A ContentNegotiationStrategy that uses the path extension of the URL to determine what media types are requested. The path extension is first looked up in the map of media types provided to the constructor. If that fails, the Java Activation framework is used as a fallback mechanism.

  The presence of the Java Activation framework is detected and enabled automatically but the `setUseJaf(boolean)` property may be used to override that setting.

  Since:
    3.2
  Author:
    Rossen Stoyanchev

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `PathExtensionContentNegotiationStrategy()` Create an instance without any mappings to start with.
    `PathExtensionContentNegotiationStrategy(Map<String,MediaType> mediaTypes)` Create an instance with the given extension-to-MediaType lookup.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected String` | `getMediaTypeKey(NativeWebRequest webRequest)` Sub-classes must extract the key to use to look up a media type.
    `protected void` | `handleMatch(String extension, MediaType mediaType)` Invoked when a matching media type is found in the lookup map.
    `protected MediaType` | `handleNoMatch(NativeWebRequest webRequest, String extension)` Invoked when no matching media type is found in the lookup map.
    `void` | `setUseJaf(boolean useJaf)` Indicate whether to use the Java Activation Framework to map from file extensions to media types.

    - ### Methods inherited from class org.springframework.web.accept.AbstractMappingContentNegotiationStrategy

      `resolveMediaTypes`

    - ### Methods inherited from class org.springframework.web.accept.MappingMediaTypeFileExtensionResolver

      `addMapping, getAllFileExtensions, lookupMediaType, resolveFileExtensions`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.springframework.web.accept.MediaTypeFileExtensionResolver

      `getAllFileExtensions, resolveFileExtensions`

- - ### Constructor Detail

    - #### PathExtensionContentNegotiationStrategy

          public PathExtensionContentNegotiationStrategy(Map<String,MediaType> mediaTypes)

      Create an instance with the given extension-to-MediaType lookup.
      Throws:
        `IllegalArgumentException` - if a media type string cannot be parsed

    - #### PathExtensionContentNegotiationStrategy

          public PathExtensionContentNegotiationStrategy()

      Create an instance without any mappings to start with. Mappings may be added later on if any extensions are resolved through the Java Activation framework.

  - ### Method Detail

    - #### setUseJaf

          public void setUseJaf(boolean useJaf)

      Indicate whether to use the Java Activation Framework to map from file extensions to media types.

      Default is `true`, i.e. the Java Activation Framework is used (if available).

    - #### getMediaTypeKey

          protected String getMediaTypeKey(NativeWebRequest webRequest)

      Description copied from class: `AbstractMappingContentNegotiationStrategy`
      Sub-classes must extract the key to use to look up a media type.
      Specified by:
        `getMediaTypeKey` in class `AbstractMappingContentNegotiationStrategy`
      Returns:
        the lookup key or `null` if the key cannot be derived

    - #### handleMatch

          protected void handleMatch(String extension,
                         MediaType mediaType)

      Description copied from class: `AbstractMappingContentNegotiationStrategy`
      Invoked when a matching media type is found in the lookup map.
      Overrides:
        `handleMatch` in class `AbstractMappingContentNegotiationStrategy`

    - #### handleNoMatch

          protected MediaType handleNoMatch(NativeWebRequest webRequest,
                                String extension)

      Description copied from class: `AbstractMappingContentNegotiationStrategy`
      Invoked when no matching media type is found in the lookup map. Sub-classes can take further steps to determine the media type.
      Overrides:
        `handleNoMatch` in class `AbstractMappingContentNegotiationStrategy`