2

如何在 Restlet(谷歌应用引擎 2.0 版)中设置内容类型?在这种情况下,我想将内容类型设置为“文本/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);

4

5 回答 5

8

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

 @Get( value = "xml" )
 public String myMethodNameHere(){
    return getSomeXml();
 }
于 2011-06-07T00:05:30.833 回答
5

如果您使用注释,您可以这样做

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

    setStatus(Status.SUCCESS_OK);

    return new StringRepresentation("Hi");
 }

请参阅GetMetadataService

于 2010-06-12T01:35:31.177 回答
3

从我前段时间写的一些代码中复制这个,不确定事情是否已经改变:

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

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

于 2010-06-12T00:34:08.377 回答
1

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

Thie 大部分时间都可以工作,但在某些浏览器中,它会返回 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;
于 2012-04-19T12:42:19.947 回答
1

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

@Get
public Representation represent() {
    StringRepresentation sr = new StringRepresentation("xml string..");
    sr.setMediaType(MediaType.APPLICATION_XML);
    return sr;
}
于 2012-10-24T15:30:28.230 回答