调用的地方
public static String save(R r){
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet request = null;
String str=(JSONArray.fromObject(r).toString())
.replace("\"", "%22").replace("{", "%7b").replace("}", "%7d")
.replace("[", "%5b").replace("]", "%5d");
request = new HttpGet(IP地址或域名+/save/(访问路径)+ str);
String s="";
HttpResponse response;
try {
response = httpclient.execute(request);
s= EntityUtils.toString(response.getEntity());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
JSONArray json = JSONArray.fromObject(s);
String flag=json.getJSONObject(0).get("msg").toString();
return flag;
};
被调用的地方
public interface RWeb extends Serializable{
@GET
@Path("/save/{jsonStr}")
@Produces({MediaType.APPLICATION_JSON + ";charset=UTF-8"})
public String saveRw(@PathParam("jsonStr")String jsonStr);
}
public class RWebImpl implements Serializable,RWeb{
private static final long serialVersionUID = -3538237361357883218L;
private RWebService rWebService;
public void setRWebService(RWebService rWebService) {
this.rWebService = rWebService;
}
@Override
public String saveR(String jsonStr) {
String s = rWebService.saveR(jsonStr);
return s;
}
}
public class RWebService extends SupportService{
MysqlQpDAO mysqldao;
public MysqlQpDAO getMysqldao() {
return mysqldao;
}
public void setdao(MysqlQpDAO mysqldao) {
this.mysqldao = mysqldao;
}
public String saveR(String jsonStr) {
try {
JSONArray rwJson = JSONArray.fromObject(jsonStr);
for(int i=0;i<rwJson.size();i++){
R r = getEntity(rwJson.getJSONObject(i).toString(), Renwu.class );
r.setZ("z");
super.save(r);
}
} catch (Exception e) {
return "[{\"msg\":\"false\"}]";
}
return "[{\"msg\":\"true\"}]";
}
/**
* jackson 将json转java类方法
* @param <T>
* @param jsonString
* @param prototype
* @return
*/
public static <T> T getEntity(String jsonString, Class<T> prototype) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return (T) objectMapper.readValue(jsonString, prototype);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}