//首先先定义一个对象类,有利于参数的封装:
public class ParamPojo {
private Object pContext;
private Object pInData;
private Object value;
public Object getpContext() {
return pContext;
}
public void setpContext(Object pContext) {
this.pContext = pContext;
}
public Object getpInData() {
return pInData;
}
public void setpInData(Object pInData) {
this.pInData = pInData;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
//参数安全验证数据封装
public static AutoPojo autoObj(){
AutoPojo auto = new AutoPojo();
auto.setUserAgent(0);
auto.setAccountType(1);
auto.setAccountName(1);
auto.setAccountPwdHash("MQ==");
return auto;
}
//将封装好的参数通过工具类进行转换
//1.将map类型的对象转换为JsonObject
public static JSONObject restPost(String URL,Map<String,Object> map,String value,RestTemplate res){
ParamPojo pojo = new ParamPojo();pojo.setpContext(autoObj());
pojo.setpInData(map);
pojo.setValue(value);
JSONObject object = JSONObject.fromObject(pojo);
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(type);
HttpEntity<String> requestEntity = new HttpEntity<String>(object.toString(), headers);
String jsonStr=res.exchange(URL,HttpMethod.POST, requestEntity,String.class).getBody();
JSONObject returnObj = JSONObject.fromObject(jsonStr);
return returnObj;
}
//2.将String类型的对象转换为JsonObject
public static JSONObject restPost(String URL,String map,String value,RestTemplate res){
ParamPojo pojo = new ParamPojo();
pojo.setpContext(autoObj());
pojo.setpInData(map);
pojo.setValue(value);
JSONObject object = JSONObject.fromObject(pojo);
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(type);
HttpEntity<String> requestEntity = new HttpEntity<String>(object.toString(), headers);
String jsonStr=res.exchange(URL,HttpMethod.POST, requestEntity,String.class).getBody();
JSONObject returnObj = JSONObject.fromObject(jsonStr);
return returnObj;
}