RestTemplate调用post请求(requestParam参数和requestBody参数)

    public void test() throws Exception{
      RestTemplate restTemplate = SpringContextHolder.getBean(RestTemplate.class);
      HttpEntity requestEntity = null;
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
      JSONArray jsonArray = JSONObject.parseArray("[\n" +
            "  {\n" +
            "    \"checkInfo\": \"16\",\n" +
            "    \"checkTypeCode\": \"AGE\"\n" +
            "  },\n" +
            "  {\n" +
            "    \"checkInfo\": \"217\",\n" +
            "    \"checkTypeCode\": \"EXAM\"\n" +
            "  }\n" +
            "\n" +
            "]");
      String[] aa = new String[]{"I21.9","DIAG"};
      requestEntity = new HttpEntity(jsonArray, headers);
      String url = "http://192.168.0.107:8088/kb-management/api/v1/kbInfoExtend/checkData";
      url += "?checkObjectType=\"DIAG\"&checkObjectCode=\"I21.9\"";
      JSONObject postResult = restTemplate.postForObject(url, requestEntity, JSONObject.class);
      int a = 1;
   }

 

你可能感兴趣的:(java后端,springboot,RestTemplate,post请求,requestParam参数,requestBody参数)