Http传输json进行服务端接口与客户端对接,以及restful实现

原文地址:https://blog.csdn.net/u011320740/article/details/53537484

话不多说,直接贴代码,照写就好了

服务端接口编写,我用的是springmvc,服务端接口其实和平时web开发一样,就是返回出json就好了,还有就是接受数据也是json,方法如下:

@Controller

@RequestMapping("/user")

public class UserController {


@Autowired

private UserService userservice;

@RequestMapping("/getUserByName")

public @ResponseBody User getUserByName(HttpServletRequest request) throws IOException{

StringBuffer str = new StringBuffer();

try {

BufferedInputStream in = new BufferedInputStream(request.getInputStream());

int i;

char c;

while ((i=in.read())!=-1) {

c=(char)i;

str.append(c);

}

}catch (Exception ex) {

ex.printStackTrace();

}

JSONObject obj= JSONObject.fromObject(str.toString());

System.out.println(obj.get("name"));

User user= userservice.getUserByName(obj.get("name").toString());

return user;

}


}

 

实现的是一个通过服务端接收客户端json{“name”:"cwh"}名字name进行查询操作,然后给客户端返回json;

 

客户端实现如下:

import java.io.IOException;


import net.sf.json.JSONObject;

import net.spring.model.User;


import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;

import org.junit.Test;


public class Client {

@Test

public void HttpPostData() {

try {

HttpClient httpclient = new DefaultHttpClient();

String uri = "http://localhost:8080/springMVC/user/getUserByName";

HttpPost httppost = new HttpPost(uri);

//添加http头信息

httppost.addHeader("Authorization", "your token"); //认证token

httppost.addHeader("Content-Type", "application/json");

httppost.addHeader("User-Agent", "imgfornote");

JSONObject obj = new JSONObject();

obj.put("name", "cwh");

httppost.setEntity(new StringEntity(obj.toString()));

HttpResponse response;

response = httpclient.execute(httppost);

//检验状态码,如果成功接收数据

int code = response.getStatusLine().getStatusCode();

System.out.println(code+"code");

if (code == 200) {

String rev = EntityUtils.toString(response.getEntity());//返回json格式: {"id": "","name": ""}

obj= JSONObject.fromObject(rev);


User user = (User)JSONObject.toBean(obj,User.class);

System.out.println("返回数据==="+user.toString());

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

}
 

ok,把服务端部署到tomcat

 

运行客户端:返回数据成功


 

当然也可以用RESTFUL风格来实现

服务端可以这么编写:

@RequestMapping("/getUserByName/{name}")

public @ResponseBody User getUserByName(@PathVariable("name")String name) throws IOException{

User user= userservice.getUserByName(name);

return user;

}
 

那么客户端请求就这么写了:

public void HttpPostData() {

try {

HttpClient httpclient = new DefaultHttpClient();

String uri = "http://localhost:8080/springMVC/user/getUserByName/cwh";

HttpPost httppost = new HttpPost(uri);

JSONObject obj = new JSONObject();


HttpResponse response;

response = httpclient.execute(httppost);

//检验状态码,如果成功接收数据

int code = response.getStatusLine().getStatusCode();

System.out.println(code+"code");

if (code == 200) {

String rev = EntityUtils.toString(response.getEntity());//返回json格式: {"id": "","name": ""}

obj= JSONObject.fromObject(rev);

User user = (User)JSONObject.toBean(obj,User.class);

System.out.println("返回数据==="+user.toString());

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

 


这样也是可以的

 

展开阅读全文

没有更多推荐了,返回首页