Questions : How does RestTemplate work?
Rest Client Program using RestTemplate
Spring configuration file......
rest-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
</bean>
</beans>
@Service("ProgramServiceImpl")
public class ProgramServiceImpl implements IProgramService{
@Autowired
@Qualifier("restTemplate")
private RestTemplate restTemplate;
@Override
public void addProgram(ProgramVO programVo) {
//Defining what output we are expecting from rest web service
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
//Here we are creating header object......................
HttpHeaders headers = new HttpHeaders();
// what output we are expecting from server = ie. rest provider
headers.setAccept(acceptableMediaTypes);
//the format of data which we are sending from client to server
headers.setContentType(MediaType.APPLICATION_JSON);
// setting header
HttpEntity<ProgramVO> requestEntity = new HttpEntity<ProgramVO>(programVo, headers);
ResponseEntity<String> response = restTemplate.exchange(
"http://www.gpsprogramys.co.in/com.gps.quiz/android/v1/program/padd", HttpMethod.POST, requestEntity,String.class);
String output = response.getBody();
}
ProgramVOWrapper .java (Wrapper class)
public class ProgramVOWrapper {
List<ProgramVO> programVOs;
public List<ProgramVO> getProgramVOs() {
return programVOs;
}
public void setProgramVOs(List<ProgramVO> programVOs) {
this.programVOs = programVOs;
}
@Override
public String toString() {
return "ProgramVOWrapper [programVOs=" + programVOs + "]";
}
}
public class ProgramVO {
private String id;
private String programTiitle;
private String code;
private String topic;
private String language;
private String level;
private Timestamp timestamp;
private String userid;
private Date cdate;
//getter and setter of all above attributes..............
}
Accessing image in Rest web service client
// Prepare acceptable media type
List<MediaType> acceptableMediaTypes = new
ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.IMAGE_JPEG);
// Prepare header
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity<String> entity = new
HttpEntity<String>(headers);
// Send the request as GET
//
/http://localhost:8090/ImageProvider/rest/images/bella
//This is calling restful web service
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<byte[]> result = restTemplate.exchange(ApplicationConstants.APPLICATION_BASE_URL+"/images/"+imageName,
HttpMethod.GET, entity, byte[].class);
Note: In above URI we are sending "imageName" as a part of rest URI
No comments:
Post a Comment