Resttemplate -- the postforobject method passes the object directly

1. We passed http://start.spring.io/ Initialize a simple spring boot project named RestTemplate. Since only RestTemplate features are studied, only web can be added, as shown in the figure. In addition, alibaba.com is added to this project Fastjson is a jar package that you need in POM XML add dependency

 

<!--Ali FastJson rely on-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.39</version>
</dependency>


2 . After importing the project, slightly modify the configuration file and change the port number in application Add {server. In the properties file Port = 9999 (you can not change it, please feel free to ~ ~), we will post the code directly below:
2.1 entity JavaBean for passing parameters

public class RequestObject {
    private int age;
    private String name;
    private String address;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "RequestObject [age=" + age + ", name=" + name + ", address=" + address + "]";
    }
}


2.2 write a controller for testing. The following three methods test to receive parameters in the format of JavaBean, Map and JSON objects

@RestController
@RequestMapping("/user")
public class TestController {
    @RequestMapping(value = "/object", method = RequestMethod.POST)
    public String postByObject(@RequestBody RequestObject beanRequest) {
        if (beanRequest == null) {
            return "FAIL";
        }
        return "SUCCESS:" + beanRequest.toString();
    }
    @RequestMapping(value = "/map", method = RequestMethod.POST)
    public String postByMap(@RequestBody Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return "FAIL";
        }
        return "SUCCESS:" + map.toString();
    }
    @RequestMapping(value = "/json", method = RequestMethod.POST)
    public String postByJsonObj(@RequestBody JSONObject jsonRequest) {
        if (jsonRequest == null || jsonRequest.isEmpty()) {
            return "FAIL";
        }
        return "SUCCESS:" + jsonRequest.toString();
    }
}

2.3 write a simple tool class to initialize the RestTemplate object

public class RestTemplateUtil {


    /**
     * Creates a RestTemplate for the specified character set
     *
     * @param charset
     * @return
     */
    public static RestTemplate getInstance(String charset) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new StringHttpMessageConverter(Charset.forName(charset)));
        return restTemplate;
    }

}


2.4 write the unit test class ResttemplateApplicationTests and add the following code

public class ResttemplateApplicationTests {
    @Test
    public void testJavaObj() {
        RequestObject request = new RequestObject();
        request.setAge(18);
        request.setName("Xiao Fang");
        request.setAddress("Shenzhen, Guangdong");
        RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");
        String url = "http://localhost:9999/test/object";
//        String url = "http://localhost:9999/test/map";
//        String url = "http://localhost:9999/test/json";
        String result = restTemplate.postForObject(url, request, String.class);
        System.out.println(result);
    }

    @Test
    public void testMap() {
        Map<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("age", 18);
        hashMap.put("name", "Xiao Fang");
        hashMap.put("address", "Shenzhen, Guangdong");
        RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");
//        String url = "http://localhost:9999/test/object";
        String url = "http://localhost:9999/test/map";
//        String url = "http://localhost:9999/test/json";
        String result = restTemplate.postForObject(url, hashMap, String.class);
        System.out.println(result);
    }

    @Test
    public void testJson() {
        JSONObject json = new JSONObject();
        json.put("age", 18);
        json.put("name", "Xiao Fang");
        json.put("address", "Shenzhen, Guangdong");
        RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");
//        String url = "http://localhost:9999/test/object";
//        String url = "http://localhost:9999/test/map";
        String url = "http://localhost:9999/test/json";
        String result = restTemplate.postForObject(url, json, String.class);
        System.out.println(result);
    }
}


3. Start the Application, and then run the ResttemplateApplicationTests test class. You will find that the three requests are sent successfully, and the console prints the success information in turn.

"D:\Program Files\Java\jdk1.8.0_121\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.6\lib\idea_rt.jar=10098:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.6\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.6\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.6\plugins\junit\lib\junit-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.6\plugins\junit\lib\junit5-rt.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;D:\ideas\spring-boot-api-project-seed-master\target\test-classes;D:\ideas\spring-boot-api-project-seed-master\target\classes;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter-web\1.5.7.RELEASE\spring-boot-starter-web-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter\1.5.7.RELEASE\spring-boot-starter-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot\1.5.7.RELEASE\spring-boot-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter-logging\1.5.7.RELEASE\spring-boot-starter-logging-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;C:\Users\usher\.m2\repository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;C:\Users\usher\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.25\jcl-over-slf4j-1.7.25.jar;C:\Users\usher\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;C:\Users\usher\.m2\repository\org\slf4j\log4j-over-slf4j\1.7.25\log4j-over-slf4j-1.7.25.jar;C:\Users\usher\.m2\repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\1.5.7.RELEASE\spring-boot-starter-tomcat-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.20\tomcat-embed-core-8.5.20.jar;C:\Users\usher\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.20\tomcat-embed-el-8.5.20.jar;C:\Users\usher\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.20\tomcat-embed-websocket-8.5.20.jar;C:\Users\usher\.m2\repository\org\hibernate\hibernate-validator\5.3.5.Final\hibernate-validator-5.3.5.Final.jar;C:\Users\usher\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;C:\Users\usher\.m2\repository\org\jboss\logging\jboss-logging\3.3.1.Final\jboss-logging-3.3.1.Final.jar;C:\Users\usher\.m2\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\usher\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.8.10\jackson-databind-2.8.10.jar;C:\Users\usher\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar;C:\Users\usher\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.8.10\jackson-core-2.8.10.jar;C:\Users\usher\.m2\repository\org\springframework\spring-web\4.3.11.RELEASE\spring-web-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-aop\4.3.11.RELEASE\spring-aop-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-beans\4.3.11.RELEASE\spring-beans-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-context\4.3.11.RELEASE\spring-context-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-webmvc\4.3.11.RELEASE\spring-webmvc-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-expression\4.3.11.RELEASE\spring-expression-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\1.5.7.RELEASE\spring-boot-starter-jdbc-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\apache\tomcat\tomcat-jdbc\8.5.20\tomcat-jdbc-8.5.20.jar;C:\Users\usher\.m2\repository\org\apache\tomcat\tomcat-juli\8.5.20\tomcat-juli-8.5.20.jar;C:\Users\usher\.m2\repository\org\springframework\spring-jdbc\4.3.11.RELEASE\spring-jdbc-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-tx\4.3.11.RELEASE\spring-tx-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter-test\1.5.7.RELEASE\spring-boot-starter-test-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-test\1.5.7.RELEASE\spring-boot-test-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-test-autoconfigure\1.5.7.RELEASE\spring-boot-test-autoconfigure-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\com\jayway\jsonpath\json-path\2.2.0\json-path-2.2.0.jar;C:\Users\usher\.m2\repository\net\minidev\json-smart\2.2.1\json-smart-2.2.1.jar;C:\Users\usher\.m2\repository\net\minidev\accessors-smart\1.1\accessors-smart-1.1.jar;C:\Users\usher\.m2\repository\org\ow2\asm\asm\5.0.3\asm-5.0.3.jar;C:\Users\usher\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\usher\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\usher\.m2\repository\org\assertj\assertj-core\2.6.0\assertj-core-2.6.0.jar;C:\Users\usher\.m2\repository\org\mockito\mockito-core\1.10.19\mockito-core-1.10.19.jar;C:\Users\usher\.m2\repository\org\objenesis\objenesis\2.1\objenesis-2.1.jar;C:\Users\usher\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\usher\.m2\repository\org\hamcrest\hamcrest-library\1.3\hamcrest-library-1.3.jar;C:\Users\usher\.m2\repository\org\skyscreamer\jsonassert\1.4.0\jsonassert-1.4.0.jar;C:\Users\usher\.m2\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;C:\Users\usher\.m2\repository\org\springframework\spring-core\4.3.11.RELEASE\spring-core-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-test\4.3.11.RELEASE\spring-test-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;C:\Users\usher\.m2\repository\org\apache\commons\commons-lang3\3.6\commons-lang3-3.6.jar;C:\Users\usher\.m2\repository\com\google\guava\guava\23.0\guava-23.0.jar;C:\Users\usher\.m2\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;C:\Users\usher\.m2\repository\com\google\errorprone\error_prone_annotations\2.0.18\error_prone_annotations-2.0.18.jar;C:\Users\usher\.m2\repository\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;C:\Users\usher\.m2\repository\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;C:\Users\usher\.m2\repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar;C:\Users\usher\.m2\repository\org\mybatis\mybatis-spring\1.3.1\mybatis-spring-1.3.1.jar;C:\Users\usher\.m2\repository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;C:\Users\usher\.m2\repository\tk\mybatis\mapper\3.4.2\mapper-3.4.2.jar;C:\Users\usher\.m2\repository\javax\persistence\persistence-api\1.0\persistence-api-1.0.jar;C:\Users\usher\.m2\repository\com\github\pagehelper\pagehelper\4.2.1\pagehelper-4.2.1.jar;C:\Users\usher\.m2\repository\com\github\jsqlparser\jsqlparser\0.9.5\jsqlparser-0.9.5.jar;C:\Users\usher\.m2\repository\com\alibaba\fastjson\1.2.39\fastjson-1.2.39.jar;C:\Users\usher\.m2\repository\com\alibaba\druid-spring-boot-starter\1.1.4\druid-spring-boot-starter-1.1.4.jar;C:\Users\usher\.m2\repository\com\alibaba\druid\1.1.4\druid-1.1.4.jar;D:\Program Files\Java\jdk1.8.0_121\lib\jconsole.jar;D:\Program Files\Java\jdk1.8.0_121\lib\tools.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\1.5.7.RELEASE\spring-boot-autoconfigure-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\freemarker\freemarker\2.3.23\freemarker-2.3.23.jar;C:\Users\usher\.m2\repository\org\mybatis\generator\mybatis-generator-core\1.3.5\mybatis-generator-core-1.3.5.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.conpany.project.ResttemplateApplicationTests
23:05:47.504 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/map"
23:05:47.506 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
23:05:47.520 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [{address=Shenzhen, Guangdong, name = Xiaofang, age = 18}] using [org. Springframework. Http. Converter. JSON MappingJackson2HttpMessageConverter@77e4c80f ]
23:05:47.547 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/map" resulted in 200 (null)
23:05:47.547 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [java.lang.String] as "text/plain;charset=UTF-8" using [org.springframework.http.converter.StringHttpMessageConverter@5af97850]
"SUCCESS:{address=Shenzhen, Guangdong, name = Xiaofang, age=18}“
23:05:47.568 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/json"
23:05:47.569 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
23:05:47.601 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [{"address":"Shenzhen, Guangdong "," name ":" Xiaofang "," age ": 18}] using [org. Springframework. Http. Converter. JSON MappingJackson2HttpMessageConverter@4313f5bc ]
23:05:47.606 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/json" resulted in 200 (null)
23:05:47.607 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [java.lang.String] as "text/plain;charset=UTF-8" using [org.springframework.http.converter.StringHttpMessageConverter@7f010382]
"SUCCESS:{\"address\":\"Guangdong Shenzhen \ ", \" name \ ": \" Xiaofang \ ", \" age\":18}"
23:05:47.611 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/object"
23:05:47.611 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
23:05:47.620 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [RequestObject [age=18, name=Xiao Fang, address=Shenzhen, Guangdong]] using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@3aefe5e5]
23:05:47.623 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/object" resulted in 200 (null)
23:05:47.623 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [java.lang.String] as "text/plain;charset=UTF-8" using [org.springframework.http.converter.StringHttpMessageConverter@2f7c2f4f]
"SUCCESS:RequestObject [age=18, name=Xiao Fang, address=Shenzhen, Guangdong]"


4. From the running results, it can be seen that the postForObject method can completely pass JavaBean s and third-party JSON objects, and the controller can successfully receive them.

5. Now think about a question: suppose our postForObject method passes in Java objects, and the controller receives them with JSON objects or Map objects, can it be called? The answer is entirely feasible. We slightly modify the first method of the test class as follows. The following method is modified to pass the JavaBean to the controller's postByMap and postByJsonObj respectively.

public void testJavaObj() {
    RequestObject request = new RequestObject();
    request.setAge(18);
    request.setName("Xiao Fang");
    request.setAddress("Shenzhen, Guangdong");
    RestTemplate restTemplate = RestTemplateUtil.getInstance("utf-8");
    String url = "http://localhost:9999/test/object";
    String url1 = "http://localhost:9999/test/map";
    String url2 = "http://localhost:9999/test/json";
    String result = restTemplate.postForObject(url, request, String.class);
    System.out.println(result);
    String result1 = restTemplate.postForObject(url1, request, String.class);
    System.out.println(result1);
    String result2 = restTemplate.postForObject(url2, request, String.class);
    System.out.println(result2);
}


Run the test method alone, and the console output is as follows. You can see that the operation is successful.

"D:\Program Files\Java\jdk1.8.0_121\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.6\lib\idea_rt.jar=11365:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.6\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.6\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.6\plugins\junit\lib\junit-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.6\plugins\junit\lib\junit5-rt.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;D:\ideas\spring-boot-api-project-seed-master\target\test-classes;D:\ideas\spring-boot-api-project-seed-master\target\classes;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter-web\1.5.7.RELEASE\spring-boot-starter-web-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter\1.5.7.RELEASE\spring-boot-starter-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot\1.5.7.RELEASE\spring-boot-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter-logging\1.5.7.RELEASE\spring-boot-starter-logging-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;C:\Users\usher\.m2\repository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;C:\Users\usher\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.25\jcl-over-slf4j-1.7.25.jar;C:\Users\usher\.m2\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;C:\Users\usher\.m2\repository\org\slf4j\log4j-over-slf4j\1.7.25\log4j-over-slf4j-1.7.25.jar;C:\Users\usher\.m2\repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\1.5.7.RELEASE\spring-boot-starter-tomcat-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.20\tomcat-embed-core-8.5.20.jar;C:\Users\usher\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.20\tomcat-embed-el-8.5.20.jar;C:\Users\usher\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.20\tomcat-embed-websocket-8.5.20.jar;C:\Users\usher\.m2\repository\org\hibernate\hibernate-validator\5.3.5.Final\hibernate-validator-5.3.5.Final.jar;C:\Users\usher\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;C:\Users\usher\.m2\repository\org\jboss\logging\jboss-logging\3.3.1.Final\jboss-logging-3.3.1.Final.jar;C:\Users\usher\.m2\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\usher\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.8.10\jackson-databind-2.8.10.jar;C:\Users\usher\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar;C:\Users\usher\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.8.10\jackson-core-2.8.10.jar;C:\Users\usher\.m2\repository\org\springframework\spring-web\4.3.11.RELEASE\spring-web-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-aop\4.3.11.RELEASE\spring-aop-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-beans\4.3.11.RELEASE\spring-beans-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-context\4.3.11.RELEASE\spring-context-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-webmvc\4.3.11.RELEASE\spring-webmvc-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-expression\4.3.11.RELEASE\spring-expression-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\1.5.7.RELEASE\spring-boot-starter-jdbc-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\apache\tomcat\tomcat-jdbc\8.5.20\tomcat-jdbc-8.5.20.jar;C:\Users\usher\.m2\repository\org\apache\tomcat\tomcat-juli\8.5.20\tomcat-juli-8.5.20.jar;C:\Users\usher\.m2\repository\org\springframework\spring-jdbc\4.3.11.RELEASE\spring-jdbc-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-tx\4.3.11.RELEASE\spring-tx-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-starter-test\1.5.7.RELEASE\spring-boot-starter-test-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-test\1.5.7.RELEASE\spring-boot-test-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-test-autoconfigure\1.5.7.RELEASE\spring-boot-test-autoconfigure-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\com\jayway\jsonpath\json-path\2.2.0\json-path-2.2.0.jar;C:\Users\usher\.m2\repository\net\minidev\json-smart\2.2.1\json-smart-2.2.1.jar;C:\Users\usher\.m2\repository\net\minidev\accessors-smart\1.1\accessors-smart-1.1.jar;C:\Users\usher\.m2\repository\org\ow2\asm\asm\5.0.3\asm-5.0.3.jar;C:\Users\usher\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\usher\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\usher\.m2\repository\org\assertj\assertj-core\2.6.0\assertj-core-2.6.0.jar;C:\Users\usher\.m2\repository\org\mockito\mockito-core\1.10.19\mockito-core-1.10.19.jar;C:\Users\usher\.m2\repository\org\objenesis\objenesis\2.1\objenesis-2.1.jar;C:\Users\usher\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\usher\.m2\repository\org\hamcrest\hamcrest-library\1.3\hamcrest-library-1.3.jar;C:\Users\usher\.m2\repository\org\skyscreamer\jsonassert\1.4.0\jsonassert-1.4.0.jar;C:\Users\usher\.m2\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;C:\Users\usher\.m2\repository\org\springframework\spring-core\4.3.11.RELEASE\spring-core-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\org\springframework\spring-test\4.3.11.RELEASE\spring-test-4.3.11.RELEASE.jar;C:\Users\usher\.m2\repository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;C:\Users\usher\.m2\repository\org\apache\commons\commons-lang3\3.6\commons-lang3-3.6.jar;C:\Users\usher\.m2\repository\com\google\guava\guava\23.0\guava-23.0.jar;C:\Users\usher\.m2\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;C:\Users\usher\.m2\repository\com\google\errorprone\error_prone_annotations\2.0.18\error_prone_annotations-2.0.18.jar;C:\Users\usher\.m2\repository\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;C:\Users\usher\.m2\repository\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;C:\Users\usher\.m2\repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar;C:\Users\usher\.m2\repository\org\mybatis\mybatis-spring\1.3.1\mybatis-spring-1.3.1.jar;C:\Users\usher\.m2\repository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;C:\Users\usher\.m2\repository\tk\mybatis\mapper\3.4.2\mapper-3.4.2.jar;C:\Users\usher\.m2\repository\javax\persistence\persistence-api\1.0\persistence-api-1.0.jar;C:\Users\usher\.m2\repository\com\github\pagehelper\pagehelper\4.2.1\pagehelper-4.2.1.jar;C:\Users\usher\.m2\repository\com\github\jsqlparser\jsqlparser\0.9.5\jsqlparser-0.9.5.jar;C:\Users\usher\.m2\repository\com\alibaba\fastjson\1.2.39\fastjson-1.2.39.jar;C:\Users\usher\.m2\repository\com\alibaba\druid-spring-boot-starter\1.1.4\druid-spring-boot-starter-1.1.4.jar;C:\Users\usher\.m2\repository\com\alibaba\druid\1.1.4\druid-1.1.4.jar;D:\Program Files\Java\jdk1.8.0_121\lib\jconsole.jar;D:\Program Files\Java\jdk1.8.0_121\lib\tools.jar;C:\Users\usher\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\1.5.7.RELEASE\spring-boot-autoconfigure-1.5.7.RELEASE.jar;C:\Users\usher\.m2\repository\org\freemarker\freemarker\2.3.23\freemarker-2.3.23.jar;C:\Users\usher\.m2\repository\org\mybatis\generator\mybatis-generator-core\1.3.5\mybatis-generator-core-1.3.5.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.conpany.project.ResttemplateApplicationTests,testJavaObj
23:30:29.349 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/object"
23:30:29.352 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
23:30:29.376 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [RequestObject [age=18, name=Xiao Fang, address = Shenzhen, Guangdong]] using [org. Springframework. Http. Converter. JSON MappingJackson2HttpMessageConverter@7d68ef40 ]
23:30:29.394 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/object" resulted in 200 (null)
23:30:29.394 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [java.lang.String] as "text/plain;charset=UTF-8" using [org.springframework.http.converter.StringHttpMessageConverter@3b2da18f]
"SUCCESS:RequestObject [age=18, name=Xiao Fang, address = Shenzhen, Guangdong]“
23:30:29.396 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/map"
23:30:29.396 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
23:30:29.396 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [RequestObject [age=18, name=Xiao Fang, address = Shenzhen, Guangdong]] using [org. Springframework. Http. Converter. JSON MappingJackson2HttpMessageConverter@7d68ef40 ]
23:30:29.399 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/map" resulted in 200 (null)
23:30:29.399 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [java.lang.String] as "text/plain;charset=UTF-8" using [org.springframework.http.converter.StringHttpMessageConverter@3b2da18f]
"SUCCESS:{address=Shenzhen, Guangdong, name = Xiaofang, age=18}“
23:30:29.399 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/json"
23:30:29.399 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
23:30:29.399 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [RequestObject [age=18, name=Xiao Fang, address=Shenzhen, Guangdong]] using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7d68ef40]
23:30:29.402 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/json" resulted in 200 (null)
23:30:29.403 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [java.lang.String] as "text/plain;charset=UTF-8" using [org.springframework.http.converter.StringHttpMessageConverter@3b2da18f]
"SUCCESS:{\"address\":\"Shenzhen, Guangdong\",\"name\":\"Xiao Fang\",\"age\":18}"


Similarly: our testMap test method can not only pass Map, but also JavaBean and JSONObject. Our testjason test method can not only pass JSONObject, but also JavaBean and Map.

6.RestTemplate provides a lot of APIs that can be used to submit requests. Why only discuss postForObject? The following is a simple analysis:

1).postForObject directly returns the response body. When we request, we restrict the type of the response body through generics, but this method can't get the status header information.

2). The postForEntity method will return the ResponseEntity object. The return value of this method contains not only the status header information, but also the response body. For example, if you want to know whether your request returns 500 or 200, please use postForEntity! ResponseEntity implements the HttpEntity interface, which provides the result returned by getBody (1).

3). The postforlocation method will return the URI object of the jdk!

4). The patchforobject method is basically the same as the postForObject method. The only difference is that it executes httpmethod Patch, which executes httpmethod POST

5). The execute method is a method directly called by postForEntity, postforebject, postForLocation or patchforebject. The return value type is different depending on the call type!

6).doExecute is a method called directly by execute. Different call types and return types are different!

7).exchange directly calls execute and returns the ResponseEntity object. It receives the HttpMethod parameter and can define the request method externally, such as post request or get request.

To sum up, the differences and relationships between the methods of postforebject, postForEntity, postForLocation, patchforebject, exchange, execute and doExecute are shown in the following figure. The cyan square represents the method, the green rounded square represents the return value type, the red arrow represents the method call direction, and the purple arrow represents the return value provided by the method.

 

A simple analysis of RestTemplate.

Keywords: Java Spring

Added by jason97673 on Fri, 24 Dec 2021 02:50:25 +0200