RestTemplate与HttpClient POST请求设置编码

以下的参数是json字符串

String json = "{\n" +
                "\t\"o\": {\n" +
                "\t\t\"items\": {\n" +
                "\t\t\t\"e\": {\n" +
                "\t\t\t\t\"prevField1\": {\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"prevField2\": {\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"qmScSampleId\": {\n" +
                "\t\t\t\t\t\"content\": 2021022420060540,\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"chkEndTime\": {\n" +
                "\t\t\t\t\t\"content\": \"2021-03-16\",\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"prevField3\": {\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"qmJobStdId\": {\n" +
                "\t\t\t\t\t\"content\": 2021011519608860,\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"class\": \"object\",\n" +
                "\t\t\t\t\"chkBeginTime\": {\n" +
                "\t\t\t\t\t\"content\": \"2021-03-16\",\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"chkOrgCode\": {\n" +
                "\t\t\t\t\t\"content\": 2021022019980113,\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"chkRptBeginTime\": {\n" +
                "\t\t\t\t\t\"content\": \"2021-03-16\",\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"qmScTaskId\": {\n" +
                "\t\t\t\t\t\"content\": 2021022420055045,\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"substructure\": {\n" +
                "\t\t\t\t\t\"e\": [{\n" +
                "\t\t\t\t\t\t\"itemName\": {\n" +
                "\t\t\t\t\t\t\t\"content\": \"绝缘 液试验\",\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"paramVal\": {\n" +
                "\t\t\t\t\t\t\t\"content\": 1,\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"unitType\": {\n" +
                "\t\t\t\t\t\t\t\"content\": \"kV\",\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"class\": \"object\",\n" +
                "\t\t\t\t\t\t\"chkCont\": {\n" +
                "\t\t\t\t\t\t\t\"content\": \"绝缘液试验-击穿电压测量\",\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"qualifiedStatus\": {\n" +
                "\t\t\t\t\t\t\t\"content\": 170046003,\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"qmScTypeId\": {\n" +
                "\t\t\t\t\t\t\t\"content\": 2021011519608861,\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"stdVal\": {\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"qmChkContId\": {\n" +
                "\t\t\t\t\t\t\t\"content\": 2021011519608925,\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"qmParamId\": {\n" +
                "\t\t\t\t\t\t\t\"content\": 2021011519608655,\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"paramName\": {\n" +
                "\t\t\t\t\t\t\t\"content\": \"击穿电压\",\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"note\": {\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"scTypeName\": {\n" +
                "\t\t\t\t\t\t\t\"content\": \"成品试验\",\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t},\n" +
                "\t\t\t\t\t\t\"qmChkItemId\": {\n" +
                "\t\t\t\t\t\t\t\"content\": 2021011519608862,\n" +
                "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t\t\t}\n" +
                "\t\t\t\t\t}],\n" +
                "\t\t\t\t\t\"class\": \"array\"\n" +
                "\t\t\t\t},\n" +
                "\t\t\t\t\"unqualifiedGrade\": {\n" +
                "\t\t\t\t\t\"content\": 170047001,\n" +
                "\t\t\t\t\t\"type\": \"string\"\n" +
                "\t\t\t\t}\n" +
                "\t\t\t},\n" +
                "\t\t\t\"class\": \"array\"\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity strEntity = new HttpEntity<>(json,headers);
        RestTemplate restTemplate = new RestTemplate();
        List> list = restTemplate.getMessageConverters();
        for (HttpMessageConverter converter : list) {
            if (converter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) converter).setDefaultCharset(Charset.forName("UTF-8"));
                break;
            }
         }
        String url = "http://localhost:17099/dky/pushResult";
        restTemplate.postForObject(url,strEntity,JSONObject.class);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setEntity(new StringEntity(json, Charsets.UTF_8));
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }

你可能感兴趣的:(RestTemplate与HttpClient POST请求设置编码)