webService

调用的地方

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;
	  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值