ClientHttpRequestFactory requestFactory = getClientHttpRequestFactory();
RestTemplate restTemplate = new RestTemplate(requestFactory);
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"));
Foo foo = restTemplate.postForObject(fooResourceUrl, request, Foo.class);
assertThat(foo, notNullValue());
assertThat(foo.getName(), is("bar"));
private static void createEmployee()
{
final String uri = "http://localhost:8080/springrestexample/employees";
EmployeeVO newEmployee = new EmployeeVO(-1, "Adam", "Gilly", "test@email.com");
RestTemplate restTemplate = new RestTemplate();
EmployeeVO result = restTemplate.postForObject( uri, newEmployee, EmployeeVO.class);
System.out.println(result);
}
public class PostForObjectDemo {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8080/spring-rest/data/saveinfo/{id}/{name}";
Map>String, String> map = new HashMap<String, String>();
map.put("id", "111");
map.put("name", "Shyam");
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
Person person= restTemplate.postForObject(url, address, Person.class, map);
System.out.println(person.getName());
System.out.println(person.getAddress().getVillage());
}
}
public class Post_postForObject_Example {
static final String URL_CREATE_EMPLOYEE = "http://localhost:8080/employee";
public static void main(String[] args) {
String empNo = "E11";
Employee newEmployee = new Employee(empNo, "Tom", "Cleck");
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_XML_VALUE);
headers.setContentType(MediaType.APPLICATION_XML);
RestTemplate restTemplate = new RestTemplate();
// Data attached to the request.
HttpEntity<Employee> requestBody = new HttpEntity<>(newEmployee, headers);
// Send request with POST method.
Employee e = restTemplate.postForObject(URL_CREATE_EMPLOYEE, requestBody, Employee.class);
if (e != null && e.getEmpNo() != null) {
System.out.println("Employee created: " + e.getEmpNo());
} else {
System.out.println("Something error!");
}
}
}
private void createEmployee() {
Employee newEmployee = new Employee("admin", "admin", "admin@gmail.com");
RestTemplate restTemplate = new RestTemplate();
Employee result = restTemplate.postForObject(CREATE_EMPLOYEE_ENDPOINT_URL, newEmployee, Employee.class);
System.out.println(result);
}
public static String getAnswer(RestTemplate restTemplate, String question) {
String url = "http://www.tuling123.com/openapi/api";
HttpHeaders headers = new HttpHeaders();
headers.add("Ocp-Apim-Subscription-Key", "3f5a37d9698744f3b40c89e2f0c94fb1");
headers.add("Content-Type", "application/x-www-form-urlencoded");
MultiValueMap<String, String> bodyMap = new LinkedMultiValueMap<>();
bodyMap.add("key", "e2e33efb4efb4e5794b48a18578384ee");
bodyMap.add("info", question);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(bodyMap, headers);
String result = restTemplate.postForObject(url, requestEntity, String.class);
return JsonPath.read(result, "$.text");
}
public void test() {
RestTemplate rt = new RestTemplate();
for (int i = 10; i< 100;i++) {
System.out.println(i%2);
RegistReq req = new RegistReq();
req.setName("" + i);
req.setSex(i%2);
req.setPhone("0000" + i);
req.setPassword(SecurityUtils.getInstance().EncryptPass("" + i));
req.setEmail(i + "@hifipi.com");
BaseModel res = rt.postForObject("http://tt.hifipi.com/regist", req, BaseModel.class);
System.out.println(res.getCode());
}
}
private void testCodeFirstUserMap(RestTemplate template, String cseUrlPrefix) {
User user1 = new User();
user1.setNames(new String[] {"u1", "u2"});
User user2 = new User();
user2.setNames(new String[] {"u3", "u4"});
Map<String, User> userMap = new HashMap<>();
userMap.put("u1", user1);
userMap.put("u2", user2);
@SuppressWarnings("unchecked")
Map<String, User> result = template.postForObject(cseUrlPrefix + "testUserMap",
userMap,
Map.class);
TestMgr.check("u1", result.get("u1").getNames()[0]);
TestMgr.check("u2", result.get("u1").getNames()[1]);
TestMgr.check("u3", result.get("u2").getNames()[0]);
TestMgr.check("u4", result.get("u2").getNames()[1]);
}
private void testCodeFirstAddDate(RestTemplate template, String cseUrlPrefix) {
Map<String, Object> body = new HashMap<>();
Date date = new Date();
body.put("date", date);
int seconds = 1;
Date result = template.postForObject(cseUrlPrefix + "addDate?seconds={seconds}",
body,
Date.class,
seconds);
TestMgr.check(new Date(date.getTime() + seconds * 1000), result);
}
public UserInfo loadUserInfo(OIDCAuthenticationToken token){
HttpClient httpClient=new DefaultHttpClient();
HttpComponentsClientHttpRequestFactory factory=new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate=new RestTemplate(factory);
MultiValueMap<String,String> form=new LinkedMultiValueMap<String,String>();
form.add("access_token",token.getAccessTokenValue());
form.add("schema","openid");
String userInfoString=restTemplate.postForObject(token.getServerConfiguration().getUserInfoUrl(),form,String.class);
JsonObject userInfoJson=new JsonParser().parse(userInfoString).getAsJsonObject();
UserInfo userInfo=DefaultUserInfo.fromJson(userInfoJson);
return userInfo;
}