草稿代码,有时间整理

   @RequestMapping(value = "/hivesd", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity getHiveSampleData(@RequestBody GetHiveRequest getHiveRequest) {

        // --------------------------Post Json-------------------------------



        String hiveTableName = tableMappingService.findHiveTableNameByUserTableName(getHiveRequest);
        RestTemplate restTemplate = new RestTemplate();

        //Http 请求URL
        String url = "http://localhost:8080/test";
        //Http Header
        HttpHeaders headers = new HttpHeaders();
        // 指定请求中的媒体类型信息
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        //Accept代表发送端(客户端)希望接受的数据类型。
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        String json = "{\"k1\":\"v1\"}";
        HttpEntity<String> entity = new HttpEntity<String>(json, headers);
        String restResult = restTemplate.postForObject(url, entity, String.class);
        System.out.println(restResult);


        //-------------------------Post Form------------------------


        url = "http://localhost:8080/testnotjson";
        headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        map.add("key", "first.last@example.com");
        map.add("key", "first.last@example.com111");
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(map, headers);
        restResult = restTemplate.postForObject(url, request, String.class);
        System.out.println(restResult);

        return null;

    }

  

posted @ 2018-01-23 14:42  bf378  阅读(458)  评论(0编辑  收藏  举报