RestTemplate

POST请求入参是JSON,发送方

public JWHandleResponse jWPersonPosition(PersonPositionRequest personPositionRequest) throws Exception {
        logger.info("请求的数据是===" + personPositionRequest);

        HttpHeaders headers = new HttpHeaders();
        //发送JSON
        headers.setContentType(MediaType.APPLICATION_JSON);
        //接收XML
        headers.add("Accept", MediaType.APPLICATION_XML_VALUE);
        headers.add("Accept-Charset", "utf-8");

        //对象转换JSON
        String body = JSON.toJSONString(personPositionRequest);
        
        HttpEntity<String> request = new HttpEntity<>(body, headers);

        String rpcResultStr = restTemplate.postForObject("http://localhost:3082/business/jw/personPosition2", request,
                String.class);

        return null;
    }
View Code

POST请求入参是XML,发送方

public JWHandleResponse jWPersonPosition(PersonPositionRequest personPositionRequest) throws Exception {
        logger.info("请求的数据是===" + personPositionRequest);

        HttpHeaders headers = new HttpHeaders();
        // 发送    XML
        headers.setContentType(MediaType.APPLICATION_XML);
        // 接收XML
        headers.add("Accept", MediaType.APPLICATION_XML_VALUE);
        headers.add("Accept-Charset", "utf-8");

        // 对象转换XML
        XmlMapper xmlMapper = new XmlMapper();
        String body = xmlMapper.writeValueAsString(personPositionRequest);
    
        HttpEntity<String> request = new HttpEntity<>(body, headers);

        String rpcResultStr = restTemplate.postForObject("http://localhost:3082/business/jw/personPosition2", request,
                String.class);

        return null;
    }
View Code

POST请求接收方,可用实体接收JSON或者XML完全取决于发送方的setContentType,返回值格式则完全取决于发送方的Accept

// 人员定位接口 出
    @PostMapping(value = "/personPosition2")
    public JWHandleResponse jWPersonPosition2(@RequestBody PersonPositionRequest personPositionRequest) {
        JWHandleResponse jWHandleResponse = new JWHandleResponse();
        jWHandleResponse.setMsg("成功");
        jWHandleResponse.setData(null);
        jWHandleResponse.setStatus("1");
        return jWHandleResponse;
    }
View Code

------------------------------------------------------------------------------------------------------------------------ 

 POST请求发送方使用表单提交一个对象

public String a() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
        headers.add("Accept-Charset", "utf-8");

        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("a", "aaa");
        map.add("b", "bbb");

        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(map, headers);

        String postForObject = restTemplate.postForObject("http://localhost:3088/simulator/aa", formEntity,
                String.class);
        return postForObject;
    }
View Code

POST请求接收方用对象接收表单提交

    @PostMapping(value = "aa")
    public String aa(Bean1 bean1) {
        return "ok";
    }
View Code

------------------------------------------------------------------------------------------------------------------------ 

GET请求发送多个参数

public String b() {
        String forObject = restTemplate.getForObject("http://localhost:3088/simulator/bbb?name=zhangsan&age=12",
                String.class);
        return forObject;
    }
View Code

GET请求用多个参数接收

@GetMapping(value = "bb")
    public String bb(String name, String age) {
        return "ok";
    }
View Code

GET请求用对象接收

@GetMapping(value = "bbb")
    public String bb(Bean2 bean2) {
        return "ok";
    }
View Code

------------------------------------------------------------------------------------------------------------------------ 

getForObject()不能设置请求头,也就意味这无法规定返回值的参数类型是JSON还是XML我这里打印的结果是XML,但我们可以在参数中规定返回值的类型。

@GetMapping(value = "c")
    public String c() {
        String forObject = restTemplate.getForObject("http://localhost:3088/simulator/cc?name=zhangsan&age=12",
                String.class);
        //<Bean2><name>zhangsan</name><age>12</age></Bean2>
        System.out.println(forObject);

        Bean2 forObject2 = restTemplate.getForObject("http://localhost:3088/simulator/cc?name=zhangsan&age=12",
                Bean2.class);
        //Bean2 [name=zhangsan, age=12]
        System.out.println(forObject2);

        return forObject;
    }

    @GetMapping(value = "cc")
    public Bean2 cc(Bean2 bean2) {
        return bean2;
    }
View Code

------------------------------------------------------------------------------------------------------------------------ 

ForEntity获取全部的响应内容,ForObject只能获取响应体,获取不到响应头

@GetMapping(value = "d")
    public String d() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Accept", MediaType.APPLICATION_XML_VALUE);
        headers.add("Accept-Charset", "utf-8");

        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("name", "aaa");
        map.add("age", "bbb");

        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(map, headers);

        ResponseEntity<String> postForEntity = restTemplate.postForEntity("http://localhost:3088/simulator/dd",
                formEntity, String.class);

        String body = postForEntity.getBody();
        //<Bean2><name>aaa</name><age>bbb</age></Bean2>
        System.out.println(body);

        int statusCodeValue = postForEntity.getStatusCodeValue();
        //200
        System.out.println(statusCodeValue);
    
        HttpHeaders headers2 = postForEntity.getHeaders();
        MediaType contentType = headers2.getContentType();
        //application/xml;charset=UTF-8
        System.out.println(contentType);
        
        return "";
    }

    @PostMapping(value = "dd")
    public Bean2 dd(Bean2 bean2) {
        return bean2;
    }
View Code
原文地址:https://www.cnblogs.com/zumengjie/p/13045242.html