Post Json 例子 Spring Rest Template & HttpClient
2014-03-13 12:04
981 查看
package com.lee;
import static java.lang.System.out;
import java.io.Serializable;
import java.io.StringWriter;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class RestClientTest {
public static void main(String[] args) throws Exception {
register();
register2();
}
private static void register() throws Exception {
RegisterForm form = new RegisterForm();
form.setId("googleId");
form.setMarket("G");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(objectToString(form), headers);
String target = "http://localhost:8080/speed/user/register";
RestTemplate temp = new RestTemplate();
ResponseEntity<String> output = temp.postForEntity(target, entity, String.class);
out.println("output ==>" + output);
out.println("output body==>" + output.getBody());
}
private static void register2() throws Exception {
RegisterForm form = new RegisterForm();
form.setId("googleId");
form.setMarket("G");
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("http://localhost:8080/speed/user/register");
org.apache.http.HttpEntity entity = new StringEntity(JsonUtils.objectToString(form), ContentType.APPLICATION_JSON);
post.setEntity(entity);
HttpResponse response = client.execute(post);
org.apache.http.HttpEntity output = response.getEntity();
out.println("status is " + response.getStatusLine());
out.println("output ==>" + EntityUtils.toString(output, Consts.UTF_8));
}
private static String objectToString(Object obj) throws Exception {
ObjectMapper mapper = new ObjectMapper();
StringWriter writer = new StringWriter();
mapper.writeValue(writer, obj);
return writer.toString();
}
private static class RegisterForm implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1056274847217485228L;
private String id;
private String market;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMarket() {
return market;
}
public void setMarket(String market) {
this.market = market;
}
}
}
import static java.lang.System.out;
import java.io.Serializable;
import java.io.StringWriter;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class RestClientTest {
public static void main(String[] args) throws Exception {
register();
register2();
}
private static void register() throws Exception {
RegisterForm form = new RegisterForm();
form.setId("googleId");
form.setMarket("G");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(objectToString(form), headers);
String target = "http://localhost:8080/speed/user/register";
RestTemplate temp = new RestTemplate();
ResponseEntity<String> output = temp.postForEntity(target, entity, String.class);
out.println("output ==>" + output);
out.println("output body==>" + output.getBody());
}
private static void register2() throws Exception {
RegisterForm form = new RegisterForm();
form.setId("googleId");
form.setMarket("G");
HttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("http://localhost:8080/speed/user/register");
org.apache.http.HttpEntity entity = new StringEntity(JsonUtils.objectToString(form), ContentType.APPLICATION_JSON);
post.setEntity(entity);
HttpResponse response = client.execute(post);
org.apache.http.HttpEntity output = response.getEntity();
out.println("status is " + response.getStatusLine());
out.println("output ==>" + EntityUtils.toString(output, Consts.UTF_8));
}
private static String objectToString(Object obj) throws Exception {
ObjectMapper mapper = new ObjectMapper();
StringWriter writer = new StringWriter();
mapper.writeValue(writer, obj);
return writer.toString();
}
private static class RegisterForm implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1056274847217485228L;
private String id;
private String market;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMarket() {
return market;
}
public void setMarket(String market) {
this.market = market;
}
}
}
相关文章推荐
- Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header,Spring RestTemplate在spr
- 一个httpclient post 的例子
- Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header
- Spring RestTemplate与apache HttpClient
- Springmvc RestTemplate 使用 && json xml rest 配置
- spring boot 使用@RestController 和 @Controller 构造常见http get ,post请求例子
- Spring RestTemplate Json 例子
- HttpClient与Spring RestTemplate
- HttpClient post json
- 【web】Spring RestTemplate提交时设置POST请求参数
- org.springframework.web.client.RestTemplate
- JSON格式注册登录查询byHttpClientDoPost
- HttpClient使用post和get时Json中文乱码解决方案
- 使用 RestTemplate 发送 post 请求后返回 json 中 ":" 变成 "=" 的解决办法
- 用httpPost对JSON发送和接收的例子
- Android发送post请求的两种写法(okHttp&&httpClient)
- Web Service笔记(七):基于Restful服务端的客户端实现(HttpClient和 RestTemplate)
- How to Send an HTTP Header With Every Request With Spring RestTemplate
- 通过HttpClient调用WebApi的Get、Post方法(返回类型限定为json格式)
- java httpclient POST和GET请求 发送JSON