使用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 entity = new HttpEntity(requestJson,headers);
        String result = restTemplate.postForObject(url, entity, String.class);
        System.out.println(result);

设置contentType解决中文乱码

MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");

你可能感兴趣的:(rest)