【发布时间】:2014-09-05 17:33:14
【问题描述】:
在我的 ServerResource 中有两个不同的“Get-Functions”
public class ApiKeyRestlet extends ServerResource {
@Get("json:json")
public ApiKeyResponse apikeyAsJson() throws RecordDoesNotExist {
...
}
@Get("text:text")
public Representation apikeyAsFile() throws RecordDoesNotExist {
...
Representation representation = new StringRepresentation(data, MediaType.TEXT_PLAIN);
Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
disposition.setFilename("apikey.properties");
representation.setDisposition(disposition);
return representation;
}
...
路径 .../apikey 绑定到 ApiKeyRestlet。如果客户端发送 Accept: text/plain 然后 apikeyAsFile 应该被调用,如果客户端发送 Accept: application/json apikeyAsJson 应该生效。但是我可以将我想要的作为 Accept-Header 发送,它总是被称为 apikeyAsJson。
我认为这与这两种格式之间的兼容性有关,但我该如何处理呢?我不想为此定义两条不同的路线。
[更新]
我通过代码调试,发现Representation的返回类型添加了默认的MediaType [*/*]。得分总是 0.5,所以总是会调用 ServerResource 中定义的第一个函数 - 似乎是一个错误???如果我定义了一个 Accept-Header,这个请求的分数应该会上升......
【问题讨论】:
-
不应该只是“json”而不是“json:json”吗?我也认为 text 在这种情况下是 txt (见restlet.com/learn/javadocs/2.2/jse/api/org/restlet/service/…)
标签: java restlet restlet-2.0