记一次RestTemplate post提交

 Map params=new HashMap<>();
        params.put("name", "admin");
        params.put("password", "admin");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity httpEntity = new HttpEntity<>(JSONObject.toJSONString(params), headers);
        RestTemplate rst = new RestTemplate();
        String url = "http://localhost:8001/login";
        ResponseEntity stringResponseEntity = rst.postForEntity(url, httpEntity, String.class);
         //返回状态
        Assert.assertEquals(200,stringResponseEntity.getStatusCodeValue());
        JSONObject repJson = JSONObject.parseObject(stringResponseEntity.getBody());
         //解析json
        Assert.assertTrue(repJson.getBoolean("success"));
        token = repJson.getString("token");

 

你可能感兴趣的:(代码笔记)