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.
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. 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 .
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
The "aha!" here, is that the function must return a Representation().
Thie will work most of the time, but in certain browsers it will return 404 with content.
getResponse().setEntity(rep);
getResponse().getEntity().setModificationDate(date);
getResponse().setStatus(Status.SUCCESS_OK);
This will show content AND a 200 status code:
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.