Java 6 接口 MultivaluedMap 有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22324091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:56:47  来源:igfitidea点击:

What use is Java 6 interface MultivaluedMap?

javadata-structuresjax-rsjava-6

提问by malatesh

What use is Java 6 interface MultivaluedMap?

Java 6 接口MultivaluedMap 有什么用?

采纳答案by Rafael Winterhalter

The interface does not belong to "Java", meaning that the interface is not a part of the core libraries. It is a part of the javax.ws.rshierarchy which is part of the JAX-RSspecification. It is used by frameworks implementing the specification such as Jersey. It is used whenever maps should refer to not only a single value but to any number of values. An example for the use would be for example the storage of a request header where you one might want to add several values per key. Or even no keys in some cases where it is easier to handle an empty list compared to a nullvalue.

该接口不属于“Java”,这意味着该接口不是核心库的一部分。它是javax.ws.rs层次结构的一部分,也是JAX-RS规范的一部分。它由实现规范的框架使用,例如Jersey。当映射不仅要引用单个值而且要引用任意数量的值时,都会使用它。一个使用示例是例如存储请求标头,您可能希望在其中为每个键添加多个值。或者在某些情况下甚至没有键,与null值相比,处理空列表更容易。

Take this HTTP-header for example:

以这个 HTTP 标头为例:

Accept-Encoding: compress;q=0.5, gzip;q=1.0

接受编码:压缩;q=0.5,gzip;q=1.0

You would model this by

你可以通过

MultivaluedMap<String, String> map = ...
map.add("Accept-Encoding", "compress;q=0.5");
map.add("Accept-Encoding", "gzip;q=1.0");

internally in Jersey. This type of multiple value storage is a common problem in Java that is addressed by other implementors of maps such as Guava.

在泽西岛内部。这种类型的多值存储是 Java 中的一个常见问题,由其他映射实现者(例如Guava)解决

This is basically what the javadoc says:

这基本上是javadoc所说的:

A map of key-values pairs. Each key can have zero or more values.

键值对映射。每个键可以有零个或多个值。

回答by Arjit

Its a map of key-values pairs. Each key can have zero or multiple values

它是键值对的映射。每个键可以有零个或多个值

public interface MultivaluedMap<K,V> extends java.util.Map<K,java.util.List<V>>

回答by LConrad

A good use of a MultivaluedMap is with UriInfo. If you are writing a REST endpoint that takes in several QueryParams, you can use UriInfo to get all of the params and extract them using the getQuery() call. For example:

MultivaluedMap 的一个很好的用途是与 UriInfo。如果您正在编写一个接受多个 QueryParams 的 REST 端点,您可以使用 UriInfo 获取所有参数并使用 getQuery() 调用提取它们。例如:

public void get(@Context UriInfo ui) {
  MultivaluedMap params = ui.getRequestUri().getQuery();
  // now do what you want with your params
}

The MultivaluedMap is helpful because you could have parameters with multiple values. For example if it was a customer database and you wanted to get several customers, your map would have the key of "customerID" and several values linked to it.

MultivaluedMap 很有用,因为您可以拥有具有多个值的参数。例如,如果它是一个客户数据库并且您想要获得多个客户,则您的地图将具有“customerID”的键和与其相关联的几个值。

回答by Chitrapal Singh

MultiValuedMap is part of javax.ws.rs.core package not Core Java. It is mainly being use in storing Headers value in request and using it

MultiValuedMap 是 javax.ws.rs.core 包的一部分,而不是 Core Java。它主要用于在请求中存储 Headers 值并使用它

private MediaType getMediaType(Class entityClass, Type entityType, MultivaluedMap headers) { final Object mediaTypeHeader = headers.getFirst("Content-Type"); .... }

private MediaType getMediaType(Class entityClass, Type entityType, MultivaluedMap headers) { final Object mediaTypeHeader = headers.getFirst("Content-Type"); .... }

Also it is quite useful in UriInfo

它在 UriInfo 中也很有用

private String getJsonpFunctionName(){
    UriInfo uriInfo = getUriInfo();
    if (uriInfo == null) {
        return null;
    }

    MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
    if (queryParameters == null) {
        return null;
    }
    return queryParameters.getFirst("jsonp");
}