设置内容/媒体类型 - Restlet

时间:2010-06-12 00:28:09

标签: java content-type restlet

如何在Restlet(Android应用引擎版本2.0)中设置内容类型?在这种情况下,我想将内容类型设置为“”text / xml“。

我有:

public class SubResource  extends ServerResource {

 @Get
 public Representation get(Representation representation){

    setStatus(Status.SUCCESS_OK);
    StringRepresentation sr = new StringRepresentation(getSomeXml());

    return sr;
 }
}

即使它是在Representation中设置的值,或者它是从ServerResource类设置的方式与返回代码相同,我也不确定。

解答:

    StringRepresentation sr = new StringRepresentation(getSomeXml());
    sr.setMediaType(MediaType.TEXT_XML);

5 个答案:

答案 0 :(得分:8)

除非GAE风格中有一些我不知道的东西,否则我认为它不需要那么复杂。这对我有用:

 @Get( value = "xml" )
 public String myMethodNameHere(){
    return getSomeXml();
 }

答案 1 :(得分:5)

如果您正在使用注释,则可以

@Get("txt")
public Representation get() {

    setStatus(Status.SUCCESS_OK);

    return new StringRepresentation("Hi");
 }

请参阅GetMetadataService

答案 2 :(得分:3)

从我之前写的一些代码中复制这个,不知道自从以后是否有变化:

Representation representation = new StringRepresentation(body, MediaType.TEXT_PLAIN);
representation.setCharacterSet(CharacterSet.UTF_8);
return representation;

根据您的需要,还有MediaType.TEXT_XML

答案 3 :(得分:1)

“啊哈!”这里,函数必须返回一个Representation()。

大部分时间你都会工作,但在某些浏览器中,它会返回404内容。

getResponse().setEntity(rep);
getResponse().getEntity().setModificationDate(date);
getResponse().setStatus(Status.SUCCESS_OK);

这将显示内容和200状态代码:

getResponse().setEntity(rep);
getResponse().getEntity().setModificationDate(date);
getResponse().setStatus(Status.SUCCESS_OK);
return rep;

答案 4 :(得分:1)

注释对我不起作用。我明确设置了内容类型。

@Get
public Representation represent() {
    StringRepresentation sr = new StringRepresentation("xml string..");
    sr.setMediaType(MediaType.APPLICATION_XML);
    return sr;
}
相关问题