ylbtech-Java-Class-C:org.springframework.http.MediaType
1.返回顶部
1.1、
  1. /*
  2. * Copyright 2002-2018 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. package org.springframework.http;
  18.  
  19. import java.io.Serializable;
  20. import java.nio.charset.Charset;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Comparator;
  25. import java.util.LinkedHashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.stream.Collectors;
  29.  
  30. import org.springframework.lang.Nullable;
  31. import org.springframework.util.Assert;
  32. import org.springframework.util.CollectionUtils;
  33. import org.springframework.util.InvalidMimeTypeException;
  34. import org.springframework.util.MimeType;
  35. import org.springframework.util.MimeTypeUtils;
  36. import org.springframework.util.StringUtils;
  37.  
  38. /**
  39. * A subclass of {@link MimeType} that adds support for quality parameters
  40. * as defined in the HTTP specification.
  41. *
  42. * @author Arjen Poutsma
  43. * @author Juergen Hoeller
  44. * @author Rossen Stoyanchev
  45. * @author Sebastien Deleuze
  46. * @author Kazuki Shimizu
  47. * @since 3.0
  48. * @see <a href="http://tools.ietf.org/html/rfc7231#section-3.1.1.1">
  49. * HTTP 1.1: Semantics and Content, section 3.1.1.1</a>
  50. */
  51. public class MediaType extends MimeType implements Serializable {
  52.  
  53. private static final long serialVersionUID = 2069937152339670231L;
  54.  
  55. /**
  56. * Public constant media type that includes all media ranges (i.e. "*/*").
  57. */
  58. public static final MediaType ALL;
  59.  
  60. /**
  61. * A String equivalent of {@link MediaType#ALL}.
  62. */
  63. public static final String ALL_VALUE = "*/*";
  64.  
  65. /**
  66. * Public constant media type for {@code application/atom+xml}.
  67. */
  68. public static final MediaType APPLICATION_ATOM_XML;
  69.  
  70. /**
  71. * A String equivalent of {@link MediaType#APPLICATION_ATOM_XML}.
  72. */
  73. public static final String APPLICATION_ATOM_XML_VALUE = "application/atom+xml";
  74.  
  75. /**
  76. * Public constant media type for {@code application/x-www-form-urlencoded}.
  77. */
  78. public static final MediaType APPLICATION_FORM_URLENCODED;
  79.  
  80. /**
  81. * A String equivalent of {@link MediaType#APPLICATION_FORM_URLENCODED}.
  82. */
  83. public static final String APPLICATION_FORM_URLENCODED_VALUE = "application/x-www-form-urlencoded";
  84.  
  85. /**
  86. * Public constant media type for {@code application/json}.
  87. * @see #APPLICATION_JSON_UTF8
  88. */
  89. public static final MediaType APPLICATION_JSON;
  90.  
  91. /**
  92. * A String equivalent of {@link MediaType#APPLICATION_JSON}.
  93. * @see #APPLICATION_JSON_UTF8_VALUE
  94. */
  95. public static final String APPLICATION_JSON_VALUE = "application/json";
  96.  
  97. /**
  98. * Public constant media type for {@code application/json;charset=UTF-8}.
  99. *
  100. * <p>This {@link MediaType#APPLICATION_JSON} variant should be used to set JSON
  101. * content type because while
  102. * <a href="https://tools.ietf.org/html/rfc7159#section-11">RFC7159</a>
  103. * clearly states that "no charset parameter is defined for this registration", some
  104. * browsers require it for interpreting correctly UTF-8 special characters.
  105. */
  106. public static final MediaType APPLICATION_JSON_UTF8;
  107.  
  108. /**
  109. * A String equivalent of {@link MediaType#APPLICATION_JSON_UTF8}.
  110. *
  111. * <p>This {@link MediaType#APPLICATION_JSON_VALUE} variant should be used to set JSON
  112. * content type because while
  113. * <a href="https://tools.ietf.org/html/rfc7159#section-11">RFC7159</a>
  114. * clearly states that "no charset parameter is defined for this registration", some
  115. * browsers require it for interpreting correctly UTF-8 special characters.
  116. */
  117. public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";
  118.  
  119. /**
  120. * Public constant media type for {@code application/octet-stream}.
  121. */
  122. public static final MediaType APPLICATION_OCTET_STREAM;
  123.  
  124. /**
  125. * A String equivalent of {@link MediaType#APPLICATION_OCTET_STREAM}.
  126. */
  127. public static final String APPLICATION_OCTET_STREAM_VALUE = "application/octet-stream";
  128.  
  129. /**
  130. * Public constant media type for {@code application/pdf}.
  131. * @since 4.3
  132. */
  133. public static final MediaType APPLICATION_PDF;
  134.  
  135. /**
  136. * A String equivalent of {@link MediaType#APPLICATION_PDF}.
  137. * @since 4.3
  138. */
  139. public static final String APPLICATION_PDF_VALUE = "application/pdf";
  140.  
  141. /**
  142. * Public constant media type for {@code application/problem+json}.
  143. * @since 5.0
  144. * @see <a href="https://tools.ietf.org/html/rfc7807#section-6.1">
  145. * Problem Details for HTTP APIs, 6.1. application/problem+json</a>
  146. */
  147. public static final MediaType APPLICATION_PROBLEM_JSON;
  148.  
  149. /**
  150. * A String equivalent of {@link MediaType#APPLICATION_PROBLEM_JSON}.
  151. * @since 5.0
  152. */
  153. public static final String APPLICATION_PROBLEM_JSON_VALUE = "application/problem+json";
  154.  
  155. /**
  156. * Public constant media type for {@code application/problem+json}.
  157. * @since 5.0
  158. * @see <a href="https://tools.ietf.org/html/rfc7807#section-6.1">
  159. * Problem Details for HTTP APIs, 6.1. application/problem+json</a>
  160. */
  161. public static final MediaType APPLICATION_PROBLEM_JSON_UTF8;
  162.  
  163. /**
  164. * A String equivalent of {@link MediaType#APPLICATION_PROBLEM_JSON_UTF8}.
  165. * @since 5.0
  166. */
  167. public static final String APPLICATION_PROBLEM_JSON_UTF8_VALUE = "application/problem+json;charset=UTF-8";
  168.  
  169. /**
  170. * Public constant media type for {@code application/problem+xml}.
  171. * @since 5.0
  172. * @see <a href="https://tools.ietf.org/html/rfc7807#section-6.2">
  173. * Problem Details for HTTP APIs, 6.2. application/problem+xml</a>
  174. */
  175. public static final MediaType APPLICATION_PROBLEM_XML;
  176.  
  177. /**
  178. * A String equivalent of {@link MediaType#APPLICATION_PROBLEM_XML}.
  179. * @since 5.0
  180. */
  181. public static final String APPLICATION_PROBLEM_XML_VALUE = "application/problem+xml";
  182.  
  183. /**
  184. * Public constant media type for {@code application/rss+xml}.
  185. * @since 4.3.6
  186. */
  187. public static final MediaType APPLICATION_RSS_XML;
  188.  
  189. /**
  190. * A String equivalent of {@link MediaType#APPLICATION_RSS_XML}.
  191. * @since 4.3.6
  192. */
  193. public static final String APPLICATION_RSS_XML_VALUE = "application/rss+xml";
  194.  
  195. /**
  196. * Public constant media type for {@code application/stream+json}.
  197. * @since 5.0
  198. */
  199. public static final MediaType APPLICATION_STREAM_JSON;
  200.  
  201. /**
  202. * A String equivalent of {@link MediaType#APPLICATION_STREAM_JSON}.
  203. * @since 5.0
  204. */
  205. public static final String APPLICATION_STREAM_JSON_VALUE = "application/stream+json";
  206.  
  207. /**
  208. * Public constant media type for {@code application/xhtml+xml}.
  209. */
  210. public static final MediaType APPLICATION_XHTML_XML;
  211.  
  212. /**
  213. * A String equivalent of {@link MediaType#APPLICATION_XHTML_XML}.
  214. */
  215. public static final String APPLICATION_XHTML_XML_VALUE = "application/xhtml+xml";
  216.  
  217. /**
  218. * Public constant media type for {@code application/xml}.
  219. */
  220. public static final MediaType APPLICATION_XML;
  221.  
  222. /**
  223. * A String equivalent of {@link MediaType#APPLICATION_XML}.
  224. */
  225. public static final String APPLICATION_XML_VALUE = "application/xml";
  226.  
  227. /**
  228. * Public constant media type for {@code image/gif}.
  229. */
  230. public static final MediaType IMAGE_GIF;
  231.  
  232. /**
  233. * A String equivalent of {@link MediaType#IMAGE_GIF}.
  234. */
  235. public static final String IMAGE_GIF_VALUE = "image/gif";
  236.  
  237. /**
  238. * Public constant media type for {@code image/jpeg}.
  239. */
  240. public static final MediaType IMAGE_JPEG;
  241.  
  242. /**
  243. * A String equivalent of {@link MediaType#IMAGE_JPEG}.
  244. */
  245. public static final String IMAGE_JPEG_VALUE = "image/jpeg";
  246.  
  247. /**
  248. * Public constant media type for {@code image/png}.
  249. */
  250. public static final MediaType IMAGE_PNG;
  251.  
  252. /**
  253. * A String equivalent of {@link MediaType#IMAGE_PNG}.
  254. */
  255. public static final String IMAGE_PNG_VALUE = "image/png";
  256.  
  257. /**
  258. * Public constant media type for {@code multipart/form-data}.
  259. */
  260. public static final MediaType MULTIPART_FORM_DATA;
  261.  
  262. /**
  263. * A String equivalent of {@link MediaType#MULTIPART_FORM_DATA}.
  264. */
  265. public static final String MULTIPART_FORM_DATA_VALUE = "multipart/form-data";
  266.  
  267. /**
  268. * Public constant media type for {@code text/event-stream}.
  269. * @since 4.3.6
  270. * @see <a href="https://www.w3.org/TR/eventsource/">Server-Sent Events W3C recommendation</a>
  271. */
  272. public static final MediaType TEXT_EVENT_STREAM;
  273.  
  274. /**
  275. * A String equivalent of {@link MediaType#TEXT_EVENT_STREAM}.
  276. * @since 4.3.6
  277. */
  278. public static final String TEXT_EVENT_STREAM_VALUE = "text/event-stream";
  279.  
  280. /**
  281. * Public constant media type for {@code text/html}.
  282. */
  283. public static final MediaType TEXT_HTML;
  284.  
  285. /**
  286. * A String equivalent of {@link MediaType#TEXT_HTML}.
  287. */
  288. public static final String TEXT_HTML_VALUE = "text/html";
  289.  
  290. /**
  291. * Public constant media type for {@code text/markdown}.
  292. * @since 4.3
  293. */
  294. public static final MediaType TEXT_MARKDOWN;
  295.  
  296. /**
  297. * A String equivalent of {@link MediaType#TEXT_MARKDOWN}.
  298. * @since 4.3
  299. */
  300. public static final String TEXT_MARKDOWN_VALUE = "text/markdown";
  301.  
  302. /**
  303. * Public constant media type for {@code text/plain}.
  304. */
  305. public static final MediaType TEXT_PLAIN;
  306.  
  307. /**
  308. * A String equivalent of {@link MediaType#TEXT_PLAIN}.
  309. */
  310. public static final String TEXT_PLAIN_VALUE = "text/plain";
  311.  
  312. /**
  313. * Public constant media type for {@code text/xml}.
  314. */
  315. public static final MediaType TEXT_XML;
  316.  
  317. /**
  318. * A String equivalent of {@link MediaType#TEXT_XML}.
  319. */
  320. public static final String TEXT_XML_VALUE = "text/xml";
  321.  
  322. private static final String PARAM_QUALITY_FACTOR = "q";
  323.  
  324. static {
  325. ALL = valueOf(ALL_VALUE);
  326. APPLICATION_ATOM_XML = valueOf(APPLICATION_ATOM_XML_VALUE);
  327. APPLICATION_FORM_URLENCODED = valueOf(APPLICATION_FORM_URLENCODED_VALUE);
  328. APPLICATION_JSON = valueOf(APPLICATION_JSON_VALUE);
  329. APPLICATION_JSON_UTF8 = valueOf(APPLICATION_JSON_UTF8_VALUE);
  330. APPLICATION_OCTET_STREAM = valueOf(APPLICATION_OCTET_STREAM_VALUE);
  331. APPLICATION_PDF = valueOf(APPLICATION_PDF_VALUE);
  332. APPLICATION_PROBLEM_JSON = valueOf(APPLICATION_PROBLEM_JSON_VALUE);
  333. APPLICATION_PROBLEM_JSON_UTF8 = valueOf(APPLICATION_PROBLEM_JSON_UTF8_VALUE);
  334. APPLICATION_PROBLEM_XML = valueOf(APPLICATION_PROBLEM_XML_VALUE);
  335. APPLICATION_RSS_XML = valueOf(APPLICATION_RSS_XML_VALUE);
  336. APPLICATION_STREAM_JSON = valueOf(APPLICATION_STREAM_JSON_VALUE);
  337. APPLICATION_XHTML_XML = valueOf(APPLICATION_XHTML_XML_VALUE);
  338. APPLICATION_XML = valueOf(APPLICATION_XML_VALUE);
  339. IMAGE_GIF = valueOf(IMAGE_GIF_VALUE);
  340. IMAGE_JPEG = valueOf(IMAGE_JPEG_VALUE);
  341. IMAGE_PNG = valueOf(IMAGE_PNG_VALUE);
  342. MULTIPART_FORM_DATA = valueOf(MULTIPART_FORM_DATA_VALUE);
  343. TEXT_EVENT_STREAM = valueOf(TEXT_EVENT_STREAM_VALUE);
  344. TEXT_HTML = valueOf(TEXT_HTML_VALUE);
  345. TEXT_MARKDOWN = valueOf(TEXT_MARKDOWN_VALUE);
  346. TEXT_PLAIN = valueOf(TEXT_PLAIN_VALUE);
  347. TEXT_XML = valueOf(TEXT_XML_VALUE);
  348. }
  349.  
  350. /**
  351. * Create a new {@code MediaType} for the given primary type.
  352. * <p>The {@linkplain #getSubtype() subtype} is set to "*", parameters empty.
  353. * @param type the primary type
  354. * @throws IllegalArgumentException if any of the parameters contain illegal characters
  355. */
  356. public MediaType(String type) {
  357. super(type);
  358. }
  359.  
  360. /**
  361. * Create a new {@code MediaType} for the given primary type and subtype.
  362. * <p>The parameters are empty.
  363. * @param type the primary type
  364. * @param subtype the subtype
  365. * @throws IllegalArgumentException if any of the parameters contain illegal characters
  366. */
  367. public MediaType(String type, String subtype) {
  368. super(type, subtype, Collections.emptyMap());
  369. }
  370.  
  371. /**
  372. * Create a new {@code MediaType} for the given type, subtype, and character set.
  373. * @param type the primary type
  374. * @param subtype the subtype
  375. * @param charset the character set
  376. * @throws IllegalArgumentException if any of the parameters contain illegal characters
  377. */
  378. public MediaType(String type, String subtype, Charset charset) {
  379. super(type, subtype, charset);
  380. }
  381.  
  382. /**
  383. * Create a new {@code MediaType} for the given type, subtype, and quality value.
  384. * @param type the primary type
  385. * @param subtype the subtype
  386. * @param qualityValue the quality value
  387. * @throws IllegalArgumentException if any of the parameters contain illegal characters
  388. */
  389. public MediaType(String type, String subtype, double qualityValue) {
  390. this(type, subtype, Collections.singletonMap(PARAM_QUALITY_FACTOR, Double.toString(qualityValue)));
  391. }
  392.  
  393. /**
  394. * Copy-constructor that copies the type, subtype and parameters of the given
  395. * {@code MediaType}, and allows to set the specified character set.
  396. * @param other the other media type
  397. * @param charset the character set
  398. * @throws IllegalArgumentException if any of the parameters contain illegal characters
  399. * @since 4.3
  400. */
  401. public MediaType(MediaType other, Charset charset) {
  402. super(other, charset);
  403. }
  404.  
  405. /**
  406. * Copy-constructor that copies the type and subtype of the given {@code MediaType},
  407. * and allows for different parameter.
  408. * @param other the other media type
  409. * @param parameters the parameters, may be {@code null}
  410. * @throws IllegalArgumentException if any of the parameters contain illegal characters
  411. */
  412. public MediaType(MediaType other, @Nullable Map<String, String> parameters) {
  413. super(other.getType(), other.getSubtype(), parameters);
  414. }
  415.  
  416. /**
  417. * Create a new {@code MediaType} for the given type, subtype, and parameters.
  418. * @param type the primary type
  419. * @param subtype the subtype
  420. * @param parameters the parameters, may be {@code null}
  421. * @throws IllegalArgumentException if any of the parameters contain illegal characters
  422. */
  423. public MediaType(String type, String subtype, @Nullable Map<String, String> parameters) {
  424. super(type, subtype, parameters);
  425. }
  426.  
  427. @Override
  428. protected void checkParameters(String attribute, String value) {
  429. super.checkParameters(attribute, value);
  430. if (PARAM_QUALITY_FACTOR.equals(attribute)) {
  431. value = unquote(value);
  432. double d = Double.parseDouble(value);
  433. Assert.isTrue(d >= 0D && d <= 1D,
  434. "Invalid quality value \"" + value + "\": should be between 0.0 and 1.0");
  435. }
  436. }
  437.  
  438. /**
  439. * Return the quality factor, as indicated by a {@code q} parameter, if any.
  440. * Defaults to {@code 1.0}.
  441. * @return the quality factor as double value
  442. */
  443. public double getQualityValue() {
  444. String qualityFactor = getParameter(PARAM_QUALITY_FACTOR);
  445. return (qualityFactor != null ? Double.parseDouble(unquote(qualityFactor)) : 1D);
  446. }
  447.  
  448. /**
  449. * Indicate whether this {@code MediaType} includes the given media type.
  450. * <p>For instance, {@code text/*} includes {@code text/plain} and {@code text/html},
  451. * and {@code application/*+xml} includes {@code application/soap+xml}, etc.
  452. * This method is <b>not</b> symmetric.
  453. * <p>Simply calls {@link #includes(MimeType)} but declared with a
  454. * {@code MediaType} parameter for binary backwards compatibility.
  455. * @param other the reference media type with which to compare
  456. * @return {@code true} if this media type includes the given media type;
  457. * {@code false} otherwise
  458. */
  459. public boolean includes(@Nullable MediaType other) {
  460. return super.includes(other);
  461. }
  462.  
  463. /**
  464. * Indicate whether this {@code MediaType} is compatible with the given media type.
  465. * <p>For instance, {@code text/*} is compatible with {@code text/plain},
  466. * {@code text/html}, and vice versa. In effect, this method is similar to
  467. * {@link #includes}, except that it <b>is</b> symmetric.
  468. * <p>Simply calls {@link #isCompatibleWith(MimeType)} but declared with a
  469. * {@code MediaType} parameter for binary backwards compatibility.
  470. * @param other the reference media type with which to compare
  471. * @return {@code true} if this media type is compatible with the given media type;
  472. * {@code false} otherwise
  473. */
  474. public boolean isCompatibleWith(@Nullable MediaType other) {
  475. return super.isCompatibleWith(other);
  476. }
  477.  
  478. /**
  479. * Return a replica of this instance with the quality value of the given {@code MediaType}.
  480. * @return the same instance if the given MediaType doesn't have a quality value,
  481. * or a new one otherwise
  482. */
  483. public MediaType copyQualityValue(MediaType mediaType) {
  484. if (!mediaType.getParameters().containsKey(PARAM_QUALITY_FACTOR)) {
  485. return this;
  486. }
  487. Map<String, String> params = new LinkedHashMap<>(getParameters());
  488. params.put(PARAM_QUALITY_FACTOR, mediaType.getParameters().get(PARAM_QUALITY_FACTOR));
  489. return new MediaType(this, params);
  490. }
  491.  
  492. /**
  493. * Return a replica of this instance with its quality value removed.
  494. * @return the same instance if the media type doesn't contain a quality value,
  495. * or a new one otherwise
  496. */
  497. public MediaType removeQualityValue() {
  498. if (!getParameters().containsKey(PARAM_QUALITY_FACTOR)) {
  499. return this;
  500. }
  501. Map<String, String> params = new LinkedHashMap<>(getParameters());
  502. params.remove(PARAM_QUALITY_FACTOR);
  503. return new MediaType(this, params);
  504. }
  505.  
  506. /**
  507. * Parse the given String value into a {@code MediaType} object,
  508. * with this method name following the 'valueOf' naming convention
  509. * (as supported by {@link org.springframework.core.convert.ConversionService}.
  510. * @param value the string to parse
  511. * @throws InvalidMediaTypeException if the media type value cannot be parsed
  512. * @see #parseMediaType(String)
  513. */
  514. public static MediaType valueOf(String value) {
  515. return parseMediaType(value);
  516. }
  517.  
  518. /**
  519. * Parse the given String into a single {@code MediaType}.
  520. * @param mediaType the string to parse
  521. * @return the media type
  522. * @throws InvalidMediaTypeException if the media type value cannot be parsed
  523. */
  524. public static MediaType parseMediaType(String mediaType) {
  525. MimeType type;
  526. try {
  527. type = MimeTypeUtils.parseMimeType(mediaType);
  528. }
  529. catch (InvalidMimeTypeException ex) {
  530. throw new InvalidMediaTypeException(ex);
  531. }
  532. try {
  533. return new MediaType(type.getType(), type.getSubtype(), type.getParameters());
  534. }
  535. catch (IllegalArgumentException ex) {
  536. throw new InvalidMediaTypeException(mediaType, ex.getMessage());
  537. }
  538. }
  539.  
  540. /**
  541. * Parse the given comma-separated string into a list of {@code MediaType} objects.
  542. * <p>This method can be used to parse an Accept or Content-Type header.
  543. * @param mediaTypes the string to parse
  544. * @return the list of media types
  545. * @throws InvalidMediaTypeException if the media type value cannot be parsed
  546. */
  547. public static List<MediaType> parseMediaTypes(@Nullable String mediaTypes) {
  548. if (!StringUtils.hasLength(mediaTypes)) {
  549. return Collections.emptyList();
  550. }
  551. String[] tokens = StringUtils.tokenizeToStringArray(mediaTypes, ",");
  552. List<MediaType> result = new ArrayList<>(tokens.length);
  553. for (String token : tokens) {
  554. result.add(parseMediaType(token));
  555. }
  556. return result;
  557. }
  558.  
  559. /**
  560. * Parse the given list of (potentially) comma-separated strings into a
  561. * list of {@code MediaType} objects.
  562. * <p>This method can be used to parse an Accept or Content-Type header.
  563. * @param mediaTypes the string to parse
  564. * @return the list of media types
  565. * @throws InvalidMediaTypeException if the media type value cannot be parsed
  566. * @since 4.3.2
  567. */
  568. public static List<MediaType> parseMediaTypes(@Nullable List<String> mediaTypes) {
  569. if (CollectionUtils.isEmpty(mediaTypes)) {
  570. return Collections.emptyList();
  571. }
  572. else if (mediaTypes.size() == 1) {
  573. return parseMediaTypes(mediaTypes.get(0));
  574. }
  575. else {
  576. List<MediaType> result = new ArrayList<>(8);
  577. for (String mediaType : mediaTypes) {
  578. result.addAll(parseMediaTypes(mediaType));
  579. }
  580. return result;
  581. }
  582. }
  583.  
  584. /**
  585. * Re-create the given mime types as media types.
  586. * @since 5.0
  587. */
  588. public static List<MediaType> asMediaTypes(List<MimeType> mimeTypes) {
  589. return mimeTypes.stream().map(MediaType::asMediaType).collect(Collectors.toList());
  590. }
  591.  
  592. /**
  593. * Re-create the given mime type as a media type.
  594. * @since 5.0
  595. */
  596. public static MediaType asMediaType(MimeType mimeType) {
  597. if (mimeType instanceof MediaType) {
  598. return (MediaType) mimeType;
  599. }
  600. return new MediaType(mimeType.getType(), mimeType.getSubtype(), mimeType.getParameters());
  601. }
  602.  
  603. /**
  604. * Return a string representation of the given list of {@code MediaType} objects.
  605. * <p>This method can be used to for an {@code Accept} or {@code Content-Type} header.
  606. * @param mediaTypes the media types to create a string representation for
  607. * @return the string representation
  608. */
  609. public static String toString(Collection<MediaType> mediaTypes) {
  610. return MimeTypeUtils.toString(mediaTypes);
  611. }
  612.  
  613. /**
  614. * Sorts the given list of {@code MediaType} objects by specificity.
  615. * <p>Given two media types:
  616. * <ol>
  617. * <li>if either media type has a {@linkplain #isWildcardType() wildcard type}, then the media type without the
  618. * wildcard is ordered before the other.</li>
  619. * <li>if the two media types have different {@linkplain #getType() types}, then they are considered equal and
  620. * remain their current order.</li>
  621. * <li>if either media type has a {@linkplain #isWildcardSubtype() wildcard subtype}, then the media type without
  622. * the wildcard is sorted before the other.</li>
  623. * <li>if the two media types have different {@linkplain #getSubtype() subtypes}, then they are considered equal
  624. * and remain their current order.</li>
  625. * <li>if the two media types have different {@linkplain #getQualityValue() quality value}, then the media type
  626. * with the highest quality value is ordered before the other.</li>
  627. * <li>if the two media types have a different amount of {@linkplain #getParameter(String) parameters}, then the
  628. * media type with the most parameters is ordered before the other.</li>
  629. * </ol>
  630. * <p>For example:
  631. * <blockquote>audio/basic &lt; audio/* &lt; */*</blockquote>
  632. * <blockquote>audio/* &lt; audio/*;q=0.7; audio/*;q=0.3</blockquote>
  633. * <blockquote>audio/basic;level=1 &lt; audio/basic</blockquote>
  634. * <blockquote>audio/basic == text/html</blockquote>
  635. * <blockquote>audio/basic == audio/wave</blockquote>
  636. * @param mediaTypes the list of media types to be sorted
  637. * @see <a href="http://tools.ietf.org/html/rfc7231#section-5.3.2">HTTP 1.1: Semantics
  638. * and Content, section 5.3.2</a>
  639. */
  640. public static void sortBySpecificity(List<MediaType> mediaTypes) {
  641. Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
  642. if (mediaTypes.size() > 1) {
  643. mediaTypes.sort(SPECIFICITY_COMPARATOR);
  644. }
  645. }
  646.  
  647. /**
  648. * Sorts the given list of {@code MediaType} objects by quality value.
  649. * <p>Given two media types:
  650. * <ol>
  651. * <li>if the two media types have different {@linkplain #getQualityValue() quality value}, then the media type
  652. * with the highest quality value is ordered before the other.</li>
  653. * <li>if either media type has a {@linkplain #isWildcardType() wildcard type}, then the media type without the
  654. * wildcard is ordered before the other.</li>
  655. * <li>if the two media types have different {@linkplain #getType() types}, then they are considered equal and
  656. * remain their current order.</li>
  657. * <li>if either media type has a {@linkplain #isWildcardSubtype() wildcard subtype}, then the media type without
  658. * the wildcard is sorted before the other.</li>
  659. * <li>if the two media types have different {@linkplain #getSubtype() subtypes}, then they are considered equal
  660. * and remain their current order.</li>
  661. * <li>if the two media types have a different amount of {@linkplain #getParameter(String) parameters}, then the
  662. * media type with the most parameters is ordered before the other.</li>
  663. * </ol>
  664. * @param mediaTypes the list of media types to be sorted
  665. * @see #getQualityValue()
  666. */
  667. public static void sortByQualityValue(List<MediaType> mediaTypes) {
  668. Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
  669. if (mediaTypes.size() > 1) {
  670. mediaTypes.sort(QUALITY_VALUE_COMPARATOR);
  671. }
  672. }
  673.  
  674. /**
  675. * Sorts the given list of {@code MediaType} objects by specificity as the
  676. * primary criteria and quality value the secondary.
  677. * @see MediaType#sortBySpecificity(List)
  678. * @see MediaType#sortByQualityValue(List)
  679. */
  680. public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) {
  681. Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
  682. if (mediaTypes.size() > 1) {
  683. mediaTypes.sort(MediaType.SPECIFICITY_COMPARATOR.thenComparing(MediaType.QUALITY_VALUE_COMPARATOR));
  684. }
  685. }
  686.  
  687. /**
  688. * Comparator used by {@link #sortByQualityValue(List)}.
  689. */
  690. public static final Comparator<MediaType> QUALITY_VALUE_COMPARATOR = (mediaType1, mediaType2) -> {
  691. double quality1 = mediaType1.getQualityValue();
  692. double quality2 = mediaType2.getQualityValue();
  693. int qualityComparison = Double.compare(quality2, quality1);
  694. if (qualityComparison != 0) {
  695. return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3
  696. }
  697. else if (mediaType1.isWildcardType() && !mediaType2.isWildcardType()) { // */* < audio/*
  698. return 1;
  699. }
  700. else if (mediaType2.isWildcardType() && !mediaType1.isWildcardType()) { // audio/* > */*
  701. return -1;
  702. }
  703. else if (!mediaType1.getType().equals(mediaType2.getType())) { // audio/basic == text/html
  704. return 0;
  705. }
  706. else { // mediaType1.getType().equals(mediaType2.getType())
  707. if (mediaType1.isWildcardSubtype() && !mediaType2.isWildcardSubtype()) { // audio/* < audio/basic
  708. return 1;
  709. }
  710. else if (mediaType2.isWildcardSubtype() && !mediaType1.isWildcardSubtype()) { // audio/basic > audio/*
  711. return -1;
  712. }
  713. else if (!mediaType1.getSubtype().equals(mediaType2.getSubtype())) { // audio/basic == audio/wave
  714. return 0;
  715. }
  716. else {
  717. int paramsSize1 = mediaType1.getParameters().size();
  718. int paramsSize2 = mediaType2.getParameters().size();
  719. return Integer.compare(paramsSize2, paramsSize1); // audio/basic;level=1 < audio/basic
  720. }
  721. }
  722. };
  723.  
  724. /**
  725. * Comparator used by {@link #sortBySpecificity(List)}.
  726. */
  727. public static final Comparator<MediaType> SPECIFICITY_COMPARATOR = new SpecificityComparator<MediaType>() {
  728.  
  729. @Override
  730. protected int compareParameters(MediaType mediaType1, MediaType mediaType2) {
  731. double quality1 = mediaType1.getQualityValue();
  732. double quality2 = mediaType2.getQualityValue();
  733. int qualityComparison = Double.compare(quality2, quality1);
  734. if (qualityComparison != 0) {
  735. return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3
  736. }
  737. return super.compareParameters(mediaType1, mediaType2);
  738. }
  739. };
  740.  
  741. }
1.2、
2.返回顶部
 
3.返回顶部
 
4.返回顶部
 
5.返回顶部
 
 
6.返回顶部
 
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

Java-Class-C:org.springframework.http.MediaType的更多相关文章

  1. Spring整合Mybatis报 java.lang.ClassNotFoundException:org.springframework.core.metrics.ApplicationStartup,即:spring的版本过高,采用RELEASE稳定版

    1.遇到的问题: 今天在弄spring整合mybatis的时候遇到一个小问题,如图所示: 简单来说:就是我的spring的xml文件没找到,我就奇了怪了,我所有的配置都没问题啊! 我pom.xml配置 ...

  2. 四、SpringBoot出现报错:java.lang.NoSuchMethodError: org.springframework.http.MediaType.equalsTypeAndSubtype(Lorg/springframework/util/MimeType;)Z

    idea启动SpringBoot项目后,出现如下错误: 2019-11-19 15:24:44.344 ERROR 39168 --- [nio-8443-exec-1] o.a.c.c.C.[.[. ...

  3. Spring 整合 Flex (BlazeDS)无法从as对象 到 Java对象转换的异常:org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.Date' to required type 'java.sql.Timestamp' for property 'wfsj'; nested exception is java.lang.Ill

    异常信息如下: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value ...

  4. 【spring mvc】后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface

    后台spring mvc接收List参数报错如下:org.springframework.beans.BeanInstantiationException: Failed to instantiate ...

  5. SSM报错:No converter found for return value of type: class java.util.ArrayList at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverter

    我使用的是SSM框架,是在编写测试RESTFUL接口的时候出现, @RequestMapping(value = "/selectAll", method = RequestMet ...

  6. Java-Class-@I:org.springframework.web.bind.annotation.RequestMapping

    ylbtech-Java-Class-@I:org.springframework.web.bind.annotation.RequestMapping 1.返回顶部   2.返回顶部 1. pack ...

  7. Java-Class-C:org.springframework.http.converter.StringHttpMessageConverter

    ylbtech-Java-Class-C:org.springframework.http.converter.StringHttpMessageConverter 1.返回顶部 1.1. impor ...

  8. Java-Class-C:org.springframework.http.HttpHeaders

    ylbtech-Java-Class-C:org.springframework.http.HttpHeaders 1.返回顶部 1.1. import org.springframework.htt ...

  9. Java-Class-C:org.springframework.http.ResponseEntity

    ylbtech-Java-Class-C:org.springframework.http.ResponseEntity 1.返回顶部 1. org.springframework.http Clas ...

随机推荐

  1. delphi 打印 PDevMode 说明

    //PDevMode = _devicemodeW;// _devicemodeW = record// dmDeviceName: array[0..CCHDEVICENAME - 1] of Wi ...

  2. Vue学习笔记【2】——Vue指令之 - v-cloak、v-text和v-html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. python之-sqlite3

    在这些 URL 中,hostname 表示 MySQL 服务所在的主机,可以是本地主机(localhost),也可以是远程服务器.数据库服务器上可以托管多个数据库,因此 database 表示要使用的 ...

  4. Win7下使用DbgPrint

    在Win7下默认DbgPrint输出信息后,使用DbgView看不到内容. 新建一个reg文件,双击导出就行了. Windows Registry Editor Version 5.00 [HKEY_ ...

  5. HTML-参考手册: Px、Em 换算工具

    ylbtech-HTML-参考手册: Px.Em 换算工具 1.返回顶部 1. Px.Em 换算工具 以下工具提供了em和px的换算工具. 第一个输入框:设置了网页默认的字体像素 (通常 16px) ...

  6. delphi基础篇之数据类型之一:1.简单类型(Simple)

    1.简单类型(Simple) 简单类型包括实数类型(Real)和有序类型(Ordinal).有序类型又包括整数类型.字符类型.布尔类型.枚举类型和子界类型等. 1-1.有序类型 有序类型是一个有序数的 ...

  7. leetcode 596 BUG笔记

    There is a table courses with columns: student and class Please list out all classes which have more ...

  8. node 创建静态web服务器(上)

    声明:本文仅用来做学习记录. 本文将使用node创建一个简单的静态web服务器. 准备工作: 首先,准备好一个类似图片中这样的页面 第一步: 创建 http 服务: const http = requ ...

  9. Day 20: 面向对象【多态,封装,反射】字符串模块导入/内置attr /包装 /授权

    面向对象,多态: 有时一个对象会有多种表现形式,比如网站页面有个按钮, 这个按钮的设计可以不一样(单选框.多选框.圆角的点击按钮.直角的点击按钮等),尽管长的不一样,但它们都有一个共同调用方式,就是o ...

  10. C# Windows服务相关

    代码及注释 ServiceController sc = new ServiceController("gupdatem"); sc.Stop();//停止服务 sc.Start( ...