我已经习惯了 MSF4J 通过发布请求获得响应。发布请求处理程序的代码如下。
@Path("/psi")
public class HelloService {
@POST
@Path("/send")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject postWithParams(AppParams params) {
String imsi = params.getImsi();
JSONObject json = new JSONObject();
json.put("imsi", imsi);
return json;
}
}
该服务被另一个应用程序调用。如下所示
String url = "http://localhost:8080/psi/send";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setConnectTimeout(5000);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
JSONObject msisdn = new JSONObject();
msisdn.put("msisdn", "94773336050");
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(msisdn.toString().getBytes("UTF-8"));
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
当我同时运行这两个应用程序时,发布请求提供了所需的输出,但同时它显示 ERROR MSF4JHttpConnectorListener 和错误控制台输出,如下所示。