JAX-RS 中的 @Produces 注释

Cod*_*roc 3 java annotations jax-rs

MediaTypes我的服务方法 产生它可能产生的其中之一pdfexcel文件或其他。

@Produces({"application/pdf","application/vnd.ms-excel"...
Run Code Online (Sandbox Code Playgroud)

我的问题

我的服务返回响应类型为application/pdfalways,即使它产生excel. 为什么?

比我重新安排MediaTypes

@Produces({"application/vnd.ms-excel","application/pdf",...
Run Code Online (Sandbox Code Playgroud)

现在它application/vnd.ms-excel再次为所有响应提供类型,为什么?

我正在com.sun.jersey为客户端使用 API 并通过使用获取类型

clientResponse.getType()
Run Code Online (Sandbox Code Playgroud)

可能我认为我误解了注释的概念@Produces

请澄清。


以下是我的服务方法的代码。

response = Response.ok((Object) file);//file is Object of File
response.header("Content-Disposition","attachment; filename="+filename);
//filename can be a.pdf b.xlsx etc
return response.build();
Run Code Online (Sandbox Code Playgroud)

Rob*_*sen 5

AcceptJAX-RS 方法应将首选内容类型基于请求标头的值。如果做不到这一点,它应该默认为第一个指定的。

虽然JAX-RS 规范在这个主题上有些模糊,但Jersey 文档在描述选择机制方面非常清楚。