-
Jun 8th, 2012, 05:18 AM
#1
Rest Template and post call
In this piece of code i need to make a post call to the server so that it will return a session id and when i make a get call the it should return the json response which was not happening . The get call is just returning a html data .
now how can i get the response json data instead of the html data.
When i call response.getBody this is the output returned.
2012-06-08 15:44:43,734 DEBUG [org.springframework.web.client.RestTemplate] - <Created GET request for "http://localhost:8080/cube">
2012-06-08 15:44:43,745 DEBUG [org.springframework.web.client.RestTemplate] - <Setting request Accept header to [text/plain, application/json, */*]>
2012-06-08 15:44:43,821 DEBUG [org.springframework.web.client.RestTemplate] - <GET request for "http://localhost:8080/cube" resulted in 200 (OK)>
2012-06-08 15:44:43,821 DEBUG [org.springframework.web.client.RestTemplate] - <Reading [java.lang.String] as "text/html;charset=ISO-8859-1" using [org.springframework.http.converter.StringHttpMessa geConverter@139af80]>
Hello WOrld
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Pentaho BI Platform</title>
<META HTTP-EQUIV="refresh" CONTENT="0;URL=./serverdocs">
</head>
<body>
</body>
</html>
@Component
public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean{
private RestTemplate restTemplate;
public RestTemplate getObject(){
return restTemplate;
}
public Class< RestTemplate > getObjectType(){
return RestTemplate.class;
}
public boolean isSingleton(){
return true;
}
public void afterPropertiesSet(){
// restTemplate = new RestTemplate(
// new HttpComponentsClientHttpRequestFactoryBasicAuth(ne w HttpHost(host, port, protocol));
}
public HttpHeaders createHeaders( String username, String password ){
return new HttpHeaders(){
{
String auth = "admin" + ":" + "admin";
byte[] encodedAuth = Base64.encodeBase64( auth.getBytes() );
String authHeader = "Basic " + new String( encodedAuth );
set( "Authorization", authHeader );
}
};
}
public static void main(String argc[]) {
String SERVER_URL = "http://localhost:8080/cube";
String getSession = "/rest/saiku/session";
RestTemplateFactory factory = new RestTemplateFactory();
RestTemplate rest = new RestTemplate();
HttpHeaders headers = factory.createHeaders("admin", "admin");
headers.setContentType(MediaType.APPLICATION_FORM_ URLENCODED);
//headers.setContentType(MediaType);
// List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
// messageConverters.add(new FormHttpMessageConverter());
// HttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
// MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
// messageConverters.add(jsonConverter);
// rest.setMessageConverters(messageConverters);
HttpEntity<String> entity = new HttpEntity<String>(headers);
// ResponseEntity<String> response = rest.exchange(SERVER_URL, HttpMethod.GET, entity, String.class);
ResponseEntity response = rest.exchange(SERVER_URL + getSession, HttpMethod.POST, entity, String.class);
System.out.println("Hello WOrld" + response);
}
}
If any further information required then i am always ready to give it.
Thanks
Nagarjun
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules