CRest / How to configure to accecpt only JSON responce
I am using CRest to deserialize a JSON stream on Android. My first steps looks very promising.
To get the JSON stream from the server and not the XML one I use the following construct:
(Accept: application/json
)
@EndPoint("http://localhost:8080/myserver/rest")
@Param(name = "Accept", value = "application/json", dest = Destination.HEADER)
public interface VersionService {
@ConnectionTimeout(10000)
@Path("/version")
VersionTO getVersion();
}
This works but it's a bit annoying to copy the "Param thing" f开发者_如何学Goor every service. Is there a better way to configure all Services at one place only to return JSON?
Well I'm afraid there isn't any simple way in the current version, but feel free to open a request on the issue tracker.
Cheers,
Laurent
I faced a similar situation where I used a custom HTTP client. In your case it could look like as follows:
DefaultHttpClient client = new DefaultHttpClient();
client.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
request.addHeader("Accept", "application/json");
}
});
CRestBuilder builder = new CRestBuilder();
builder.expectsJson();
builder.setRestService(new HttpClientRestService(client));
Another option is to set default parameter for ClientPNames.DEFAULT_HEADERS of the custom HttpClient instance. Details can be found on http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html.
精彩评论