1、Web服务器是Spring MVC框架:
package com.acce.server.order.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.acce.server.core.controller.BaseController;
import com.acce.server.pcOrder.service.PcBillService;
import com.alibaba.fastjson.JSONObject;
@Controller()
public class TestController extends BaseController {
@Resource(name = "PcBillService")
private PcBillService pcBillService;
/**
* 获取tabFirster表中属于该用户的信息。
*
* @param json
* @param response
*/
@RequestMapping("/api/order/querryTabHeadByPC.html")
public void querryTabHead(@RequestParam String json,
HttpServletResponse response) {
System.out.println("/api/order/querryTabHead.html 接口获取到json:" + json);
String rstString = pcBillService.querryTabHead(
JSONObject.parseObject(json)).toString();
response.setContentType("text/html; charset=utf-8");
this.wirteJsonToResponse(response, rstString);
}
}
2、将响应结果返回:
package com.acce.server.core.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class BaseController {
protected Logger LOGGER = Logger.getLogger(this.getClass());
/**
* 将json写到servlet输出流
* @param response
* @param json
*/
protected void wirteJsonToResponse(HttpServletResponse response,String json) {
PrintWriter writer = null;
try {
writer = response.getWriter();
writer.println(json);
writer.flush();
} catch (IOException e) {
LOGGER.error("服务端写json报错:"+json,e);
} finally {
if(null != writer) {
writer.close();
}
}
}
}
3、新起一个JavaProject,导入HttpClient相关jar包:httpclient-4.3.6.jar、httpclient-cache-4.2.jar、httpcore-4.3.3.jar、httpcore-ab-4.2.1.jar、httpcore-nio-4.2.1.jar、httpmime-4.2.jar
package acce;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
public class TestHttpClient {
public static void main(String[] args) {
// \"{\"tabCorpFirst\":\"快来大酒店\",\"syncing\":\"1\"}\"
String url = "http://localhost:8080/acce-server/api/order/querryKingdomFromFirster.html";
try {
HttpClient httpClient = new DefaultHttpClient();
// HttpPost 实现 HttpUriRequest 接口,HttpUriRequest接口 继承 HttpRequest
HttpPost httpPostReq = new HttpPost(url);
ArrayList<NameValuePair> paramList = new ArrayList<NameValuePair>();
String json = "{\"tabCorpFirst\":\"快来大酒店\",\"syncing\":\"1\"}";
NameValuePair param1 = new BasicNameValuePair("json", json);
paramList.add(param1);
UrlEncodedFormEntity aa = new UrlEncodedFormEntity(paramList,
HTTP.UTF_8);
httpPostReq.setEntity(aa);
HttpResponse resp = httpClient.execute(httpPostReq);
if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(resp.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String inputLine = null;
while ((inputLine = reader.readLine()) != null) {
result.append(inputLine);
}
System.out.println("结果: " + result.toString());
} else {
System.out.println("结果: "
+ resp.getStatusLine().getStatusCode());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试调用接口正确,并能获取到正确的值;
注:由于项目是UTF-8,需要转码。