Jersey @Consumes 端点如何匹配?
How are Jersey @Consumes endpoints matched?
我正在设计一个 RESTful 端点来接收文件。我希望它接受 POSTing 作为普通 file/stream 和多部分。 servlet 容器如何匹配端点有规则吗?下面的代码能可靠地工作吗,或者这个实现是特定的?我可以使用 WILDCARD,还是必须将其限制为 APPLICATION_OCTET_STREAM?
@Path("foo")
public class Foo {
@POST
@Path("{filename}")
@Consumes(MediaType.WILDCARD)
public Response uploadFileDirect(
@PathParam("filename") String filename,
InputStream is)
{
// process input stream
Response.ok().build();
}
@POST
@Path("{filename}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFileMultipart(
@PathParam("filename") String filename,
@FormDataParam("file") InputStream is)
{
// process input stream
Response.ok().build();
}
}
这个在JAX-RS spec3.7.2请求匹配中有规定
[...]
Resource class/object is found and all resource and sub resource methods are put into set M
[...]
- Identify the method that will handle the request:
a. Filter M
by removing members that do not meet the following criteria:
[...]
b. Sort M
in descending order as follows:
* The primary key is the media type of input data. Methods whose @Consumes
value is the best match for the media type of the request are sorted first.
* The secondary key is the @Produces
value. Methods whose value of @Produces
best matches the value of the request accept header are sorted first.
Determining the best matching media types follows the general rule: n/m
> n/*
> */*
, i.e. a method that explicitly consumes the request media type or produces one of the requested media types is sorted before a method that consumes or produces */*
.
如果您查看最后一段(确定最佳匹配),它表示 */*
(MediaType.WILDCARD
) 的优先级最低。更具体的媒体类型将永远获胜。
我正在设计一个 RESTful 端点来接收文件。我希望它接受 POSTing 作为普通 file/stream 和多部分。 servlet 容器如何匹配端点有规则吗?下面的代码能可靠地工作吗,或者这个实现是特定的?我可以使用 WILDCARD,还是必须将其限制为 APPLICATION_OCTET_STREAM?
@Path("foo")
public class Foo {
@POST
@Path("{filename}")
@Consumes(MediaType.WILDCARD)
public Response uploadFileDirect(
@PathParam("filename") String filename,
InputStream is)
{
// process input stream
Response.ok().build();
}
@POST
@Path("{filename}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFileMultipart(
@PathParam("filename") String filename,
@FormDataParam("file") InputStream is)
{
// process input stream
Response.ok().build();
}
}
这个在JAX-RS spec3.7.2请求匹配中有规定
[...]
Resource class/object is found and all resource and sub resource methods are put into set
M
[...]
- Identify the method that will handle the request:
a. FilterM
by removing members that do not meet the following criteria:
[...]
b. SortM
in descending order as follows:
* The primary key is the media type of input data. Methods whose@Consumes
value is the best match for the media type of the request are sorted first.
* The secondary key is the@Produces
value. Methods whose value of@Produces
best matches the value of the request accept header are sorted first.Determining the best matching media types follows the general rule:
n/m
>n/*
>*/*
, i.e. a method that explicitly consumes the request media type or produces one of the requested media types is sorted before a method that consumes or produces*/*
.
如果您查看最后一段(确定最佳匹配),它表示 */*
(MediaType.WILDCARD
) 的优先级最低。更具体的媒体类型将永远获胜。