post 호출 인터페이스,json 인 자 를 가 져 와 json 코드 노트 를 되 돌려 줍 니 다.

	/**
	 * post     
	 */
	@Override
	public void getinterfaceByYC(XXX base) {
	    String ywbm = base.getxxx();
		String bljd = base.getXX();
		String username = "";
		String password = "";
		
		String url = "http://xxxx";

		byte[] requestBytes;
		String result = "";
		try {
			JSONObject js = new JSONObject();
			js.put("xxx", xxx);
		        js.put("xx", xx);

			String string = js.toString();

			requestBytes = string.getBytes("utf-8");
			HttpClient httpClient = new HttpClient();//       
			PostMethod postMethod = new PostMethod(url);
			//      Authorization
			// postMethod.setRequestHeader("Authorization", "Basic " +
			// authorization);
			//       Content-Type
			postMethod.setRequestHeader("Content-Type", "application/json");
			InputStream inputStream = new ByteArrayInputStream(requestBytes, 0,
					requestBytes.length);
			RequestEntity requestEntity = new InputStreamRequestEntity(
					inputStream, requestBytes.length,
					"application/json; charset=utf-8"); //    
			postMethod.setRequestEntity(requestEntity);
			httpClient.executeMethod(postMethod);//     
			InputStream soapResponseStream = postMethod
					.getResponseBodyAsStream();//       
			byte[] datas = null;
			datas = readInputStream(soapResponseStream);//          
			result = new String(datas, "UTF-8");//        String
			//       
			System.out.println("result:------" + result);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} //          
		catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/**
		 * String result = {"aaa":"111","bbb":"222"}
		 *    JSON     
		 */
		
		if (result != null && !"".equals(result)) {
			result = "[" + result + "]";
			JSONArray jsonarray = JSONArray.fromObject(result);
			String jsonString = jsonarray.getString(0);
			Map deviceMap = new HashMap<>();
			Gson gson = new Gson();
			deviceMap = gson.fromJson(jsonString, deviceMap.getClass());
			System.out.println(deviceMap.get("aaa"));
		}
		System.out.println(result);
	}


	/**
	 *          
	 */
	private byte[] readInputStream(InputStream soapResponseStream) {
		byte[] buffer = new byte[1024];
		int len = -1;
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			while ((len = soapResponseStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, len);
			}
			outputStream.close();
			soapResponseStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return outputStream.toByteArray();
	}

호출 된 인터페이스 쓰기
   @ResponseBody
    @PostMapping("xxxx")
    public JSONObject update_start(@RequestBody JSONObject jsonObject){
        System.out.println("1111111");
        System.out.println(jsonObject.toString());

        JSONObject json = new JSONObject();
        json.put("aaa","111");
        json.put("bbb","222");
        System.out.println(json.toString());
        return json;
    }

좋은 웹페이지 즐겨찾기