RestTemplate invoke JSON and convert to Object

@Test
    public void webTest() throws JsonProcessingException {

        Map<String, String> a = new HashMap<>();

        a.put("userName", "ff");
        a.put("password", "fff");

        ObjectMapper b = new ObjectMapper();

        String as = b.writeValueAsString(a);

        HttpHeaders headers2 = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers2.setContentType(type);
        headers2.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity<String> entity = new HttpEntity<String>(as, headers2);

        Object ss = restTemplate.postForObject("http://localhost:" + port + "/api/admin/login2", entity, Object.class);
        System.out.println("port is: " + port);
    }

猜你喜欢

转载自www.cnblogs.com/wblade/p/10625191.html