The input parameter of the POST request is JSON and the sender

public JWHandleResponse jWPersonPosition(PersonPositionRequest personPositionRequest) throws Exception { Logger. info(" Requested data is ===" + personPositionRequest); HttpHeaders headers = new HttpHeaders(); // Send headers. SetContentType (MediaType.APPLICATION_JSON); // Receive XML headers. Add ("Accept", MediaType.APPLICATION_XML_VALUE); headers.add("Accept-Charset", "utf-8"); JSONString 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; }Copy the code

View Code

The INPUT to the POST request is XML and the sender

public JWHandleResponse jWPersonPosition(PersonPositionRequest personPositionRequest) throws Exception { Logger. info(" Requested data is ===" + personPositionRequest); HttpHeaders headers = new HttpHeaders(); // Send XML headers. SetContentType (MediaType.APPLICATION_XML); // Receive XML headers. Add ("Accept", MediaType.APPLICATION_XML_VALUE); headers.add("Accept-Charset", "utf-8"); 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; }Copy the code

View Code

POST requests the receiver, and the entity can receive JSON or XML depending on the sender’s setContentType, and the return value format depends entirely on the sender’s Accept

@postMapping (value = "/personPosition2") public JWHandleResponse jWPersonPosition2(@RequestBody PersonPositionRequest personPositionRequest) { JWHandleResponse jWHandleResponse = new JWHandleResponse(); JWHandleResponse. SetMsg (" success "); jWHandleResponse.setData(null); jWHandleResponse.setStatus("1"); return jWHandleResponse; }Copy the code

View Code

———————————————————————————————————————— 

POST requests the sender to submit an object using the form

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;
    }
Copy the code

View Code

POST requests the recipient to receive the form submission with an object

    @PostMapping(value = "aa")
    public String aa(Bean1 bean1) {
        return "ok";
    }
Copy the code

View Code

———————————————————————————————————————— 

GET requests send multiple parameters

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

View Code

GET requests are received with multiple parameters

@GetMapping(value = "bb")
    public String bb(String name, String age) {
        return "ok";
    }
Copy the code

View Code

GET requests are received with objects

@GetMapping(value = "bbb")
    public String bb(Bean2 bean2) {
        return "ok";
    }
Copy the code

View Code

———————————————————————————————————————— 

GetForObject () does not set the request header, which means it cannot specify whether the parameter type of the returned value is JSON or XML. The result I printed here is XML, but we can specify the type of the returned value in the parameter.

@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; }Copy the code

View Code

———————————————————————————————————————— 

ForEntity gets all the response content, ForObject can only get the response body, not the response header

@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; }Copy the code

View Code