Hi Qing,
The REST API does not really support content negotiation and everything
is returned as JSON with a plain text content type. I believe this was
done mainly so that formatting could be preserved when viewing in a
browser. If you have a requirement for application/json type responses
you could open a Jira and we can look into supporting it.
Thanks,
Tom
On 6/12/14 12:12 AM, Qing Chi 79624 wrote:
Hi guys,
Following is content of TestAmbariRestApi class. Why
the Content-Type always is text/plain on the response using ambari
REST API?
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.api.json.JSONConfiguration;
import com.vmware.bdd.api.model.ApiClusterList;
import java.net.URI;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
public class TestAmbariRestApi {
private static URI getBaseURI() {
return UriBuilder.fromUri("http://10.141.73.63:8080/api/v1").build();
}
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
Boolean.TRUE);
Client client = Client.create(config);
client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
WebResource resource = client.resource(getBaseURI());
ClientResponse response;
response =
resource.path("clusters").accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
System.out.println(response.getStatus());
System.out.println(response.getHeaders().get("Content-Type"));
System.out.println(response.getEntity(ApiClusterList.class));
}
}
Result of running TestAmbariRestApi,
406
[text/plain;charset=ISO-8859-1]
Jun 12, 2014 11:58:43 AM com.sun.jersey.api.client.ClientResponse
getEntity
SEVERE: A message body reader for Java class
com.vmware.bdd.api.model.ApiClusterList, and Java type class
com.vmware.bdd.api.model.ApiClusterList, and MIME media type
text/plain; charset=ISO-8859-1 was not found
Jun 12, 2014 11:58:43 AM com.sun.jersey.api.client.ClientResponse
getEntity
SEVERE: The registered message body readers compatible with the MIME
media type are:
text/plain; charset=ISO-8859-1 ->
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ReaderProvider
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
Exception in thread "main"
com.sun.jersey.api.client.ClientHandlerException: A message body
reader for Java class com.vmware.bdd.api.model.ApiClusterList, and
Java type class com.vmware.bdd.api.model.ApiClusterList, and MIME
media type text/plain; charset=ISO-8859-1 was not found
at
com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:630)
at
com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:586)
at com.vmware.bdd.api.TestAmbariRestAPI.main(TestAmbariRestAPI.java:37)
And I used the following command, and the Content-Type is text/plain
on response.
curl -H "Content-Type: application/json" -H "X-Requested-By: ambari"
-X GET --user "admin:admin" -i http://10.141.73.63:8080/api/v1/clusters/
HTTP/1.1 200 OK
Set-Cookie: AMBARISESSIONID=1i36z3duc4cjr561r6deiub8;Path=/
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: text/plain
Content-Length: 422
Server: Jetty(7.6.7.v20120910)
{
"href" : "http://10.141.73.63:8080/api/v1/clusters/",
"items" : [
{
"href" : "http://10.141.73.63:8080/api/v1/clusters/test",
"Clusters" : {
"cluster_name" : "test",
"version" : "HDP-2.1"
}
}
]
}
Anybody give me some help?
Thanks,
-qing
--
CONFIDENTIALITY NOTICE
NOTICE: This message is intended for the use of the individual or entity to
which it is addressed and may contain information that is confidential,
privileged and exempt from disclosure under applicable law. If the reader
of this message is not the intended recipient, you are hereby notified that
any printing, copying, dissemination, distribution, disclosure or
forwarding of this communication is strictly prohibited. If you have
received this communication in error, please contact the sender immediately
and delete it from your system. Thank You.