使用resttemplate post json

實例

String url = "http://demo/api/book/";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        String requestJson = "{...}";
        HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
        String result = restTemplate.postForObject(url, entity, String.class);
        System.out.println(result);

設置contentType解決中文亂碼

MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
相關文章
相關標籤/搜索