views:

73

answers:

2

How do you set the content type in Restlet (version 2.0 for google app engine)? In this case, I'd like to set the content type to ""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.

ANSWER:

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

+1  A: 

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

Lauri Lehtinen
+1  A: 

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.

Everett Toews