当前位置:   article > 正文

初探RestTemplate--postForObject方法直接传递对象

postforobject

1.我们通过 http://start.spring.io/ 初始化一个简单的spring boot工程,取名 resttemplate, 由于只研究RestTemplate的特性,故仅添加web即可,如图所示,另外本项目额外添加了alibaba.fastjson这个jar包,你需要在pom.xml添加依赖

<!--阿里 FastJson依赖-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.39</version>
</dependency>
2 .将项目导入后,稍微修改下配置文件改改端口号,在 application.properties文件中添加   server.port = 9999 (你也可以不改,请随意即可~~),下面我们直接贴代码:
2.1用于传递参数的实体JavaBean
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编写用于测试的controller,下面三个方法分别测试以JavaBean,Map和JSON对象的格式来接收参数

@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编写简单的工具类初始化RestTemplate对象

public class RestTemplateUtil {


    /**
     * 创建指定字符集的RestTemplate
     *
     * @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编写单元测试类ResttemplateApplicationTests,并添加以下代码

public class ResttemplateApplicationTests {
    @Test
    public void testJavaObj() {
        RequestObject request = new RequestObject();
        request.setAge(18);
        request.setName("小芳");
        request.setAddress("广东深圳");
        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", "小芳");
        hashMap.put("address", "广东深圳");
        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", "小芳");
        json.put("address", "广东深圳");
        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.启动Application,然后运行ResttemplateApplicationTests测试类,你将发现三个请求均发送成功,控制台依次打印了成功信息。

  1. "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
  2. 23:05:47.504 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/map"
  3. 23:05:47.506 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
  4. 23:05:47.520 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [{address=广东深圳, name=小芳, age=18}] using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@77e4c80f]
  5. 23:05:47.547 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/map" resulted in 200 (null)
  6. 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]
  7. "SUCCESS:{address=广东深圳, name=小芳, age=18}"
  8. 23:05:47.568 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/json"
  9. 23:05:47.569 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
  10. 23:05:47.601 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [{"address":"广东深圳","name":"小芳","age":18}] using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@4313f5bc]
  11. 23:05:47.606 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/json" resulted in 200 (null)
  12. 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]
  13. "SUCCESS:{\"address\":\"广东深圳\",\"name\":\"小芳\",\"age\":18}"
  14. 23:05:47.611 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/object"
  15. 23:05:47.611 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
  16. 23:05:47.620 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [RequestObject [age=18, name=小芳, address=广东深圳]] using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@3aefe5e5]
  17. 23:05:47.623 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/object" resulted in 200 (null)
  18. 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]
  19. "SUCCESS:RequestObject [age=18, name=小芳, address=广东深圳]"

4.通过运行结果可以看出postForObject方法完全可以传递JavaBean和第三方JSON对象,而controller完全能够成功接收。

5.现在思考一个问题:假设我们的postForObject方法传入的是Java对象,而controller用JSON对象或者Map对象来接收,是否可以调通呢?答案是完全可行的。我们对测试类第一个方法稍作修改如下。以下这个方法被改造成,将JavaBean分别传递给controller的postByMap和postByJsonObj来接收。

public void testJavaObj() {
    RequestObject request = new RequestObject();
    request.setAge(18);
    request.setName("小芳");
    request.setAddress("广东深圳");
    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);
}

单独跑这个测试方法控制台输出如下,可以看出运行成功。

  1. "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
  2. 23:30:29.349 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/object"
  3. 23:30:29.352 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
  4. 23:30:29.376 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [RequestObject [age=18, name=小芳, address=广东深圳]] using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7d68ef40]
  5. 23:30:29.394 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/object" resulted in 200 (null)
  6. 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]
  7. "SUCCESS:RequestObject [age=18, name=小芳, address=广东深圳]"
  8. 23:30:29.396 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/map"
  9. 23:30:29.396 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
  10. 23:30:29.396 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [RequestObject [age=18, name=小芳, address=广东深圳]] using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7d68ef40]
  11. 23:30:29.399 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/map" resulted in 200 (null)
  12. 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]
  13. "SUCCESS:{address=广东深圳, name=小芳, age=18}"
  14. 23:30:29.399 [main] DEBUG org.springframework.web.client.RestTemplate - Created POST request for "http://localhost:9999/test/json"
  15. 23:30:29.399 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, text/plain, */*, */*]
  16. 23:30:29.399 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [RequestObject [age=18, name=小芳, address=广东深圳]] using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7d68ef40]
  17. 23:30:29.402 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9999/test/json" resulted in 200 (null)
  18. 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]
  19. "SUCCESS:{\"address\":\"广东深圳\",\"name\":\"小芳\",\"age\":18}"

同理:我们的testMap的测试方法不仅可以传递Map也可以传JavaBean和JSONObejct,我们的testJson测试方法不仅可以传JSONObject,也可以传JavaBean和Map

6.RestTemplate提供了一大堆api好像都可以来用提交请求,为什么只讨论postForObject呢?以下是简单分析:

1).postForObject直接返回响应体,我们请求时通过泛型约束响应体的类型,但是这个方法无法得到状态头信息。

2).postForEntity方法将返回ResponseEntity对象,这个方法的返回值不仅包含状态头信息还包含响应体,例如你想知道自己的请求是返回500还是200,那么请使用postForEntity!且ResponseEntity实现了HttpEntity接口,它提供getBody返回1)的结果。

3).postForLocation方法将返回jdk的URI对象!

4).patchForObject方法同postForObject基本一致,唯一区别是一个执行HttpMethod.PATCH,一个执行的是HttpMethod.POST

5).execute方法是postForEntity、postForObject、postForLocation或patchForObject直接调用的方法,调用类型不同返回值类型就不同!

6).doExecute是execute直接调用的方法,调用类型不同返回类型就不同!

7).exchange直接调用execute,返回ResponseEntity对象,它接收HttpMethod参数,可以从外部定义请求方式,例如post请求或者get请求。

总结下,关于postForObject,postForEntity,postForLocation,patchForObject,exchange,execute,doExecute各个方法的区别和联系如下图所示。青色方块表示方法,绿色圆角方块表示返回值类型,红色箭头表示方法调用方向,紫色箭头表示该方法提供的返回值。


关于RestTemplate的简单分析,暂时到此结束,谢谢观看。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/302802
推荐阅读
相关标签
  

闽ICP备14008679号