[英]Set Content/Media type - Restlet
How do you set the content type in Restlet (version 2.0 for google app engine)? 如何在Restlet(谷歌应用引擎版本2.0)中设置内容类型? In this case, I'd like to set the content type to ""text/xml".
在这种情况下,我想将内容类型设置为“”text / xml“。
I have: 我有:
public class SubResource extends ServerResource {
@Get
public Representation get(Representation representation){
setStatus(Status.SUCCESS_OK);
StringRepresentation sr = new StringRepresentation(getSomeXml());
return sr;
}
}
I'm unsure even if it is a value that is set in the Representation, or if it is set from the ServerResource class the same way that the return code is. 即使它是在Representation中设置的值,或者它是从ServerResource类设置的方式与返回代码相同,我也不确定。
StringRepresentation sr = new StringRepresentation(getSomeXml());
sr.setMediaType(MediaType.TEXT_XML);
Unless there is something in the GAE style that I don't know about, I don't think it needs to be that complicated. 除非GAE风格中有一些我不知道的东西,否则我认为它不需要那么复杂。 This works for me:
这对我有用:
@Get( value = "xml" )
public String myMethodNameHere(){
return getSomeXml();
}
If you're using annotations you could do 如果您正在使用注释,则可以执行此操作
@Get("txt")
public Representation get() {
setStatus(Status.SUCCESS_OK);
return new StringRepresentation("Hi");
}
See Get and MetadataService . 请参阅Get和MetadataService 。
Copying this from some code I wrote a while ago, not sure if things have changed since: 从我之前写的一些代码中复制这个,不知道自从以后事情是否发生了变化:
Representation representation = new StringRepresentation(body, MediaType.TEXT_PLAIN);
representation.setCharacterSet(CharacterSet.UTF_8);
return representation;
For your needs, there's also MediaType.TEXT_XML 根据您的需要,还有MediaType.TEXT_XML
The "aha!" “啊哈!” here, is that the function must return a Representation().
这里,函数必须返回一个Representation()。
Thie will work most of the time, but in certain browsers it will return 404 with content. 这将在大多数时间工作,但在某些浏览器中它将返回404内容。
getResponse().setEntity(rep);
getResponse().getEntity().setModificationDate(date);
getResponse().setStatus(Status.SUCCESS_OK);
This will show content AND a 200 status code: 这将显示内容和200状态代码:
getResponse().setEntity(rep);
getResponse().getEntity().setModificationDate(date);
getResponse().setStatus(Status.SUCCESS_OK);
return rep;
Annotation didn't work for me. 注释对我不起作用。 I set the content type explicitly.
我明确设置了内容类型。
@Get
public Representation represent() {
StringRepresentation sr = new StringRepresentation("xml string..");
sr.setMediaType(MediaType.APPLICATION_XML);
return sr;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.