springboot中RestTemplate的测试

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

controller

    @GetMapping("/ping")
    public Object ping(@RequestParam("id") String id, @RequestParam("name") String name,
                       @RequestParam("img_1") String img_1, @RequestParam("img_2") String img_2,
                       @RequestParam("detecttype") String detecttype){
        return "pong!中文+" + id + name + "img_1:" + img_1 + "img_2:" + img_2 + detecttype;
    }

    @GetMapping("/ping2")
    public String ping2(@RequestParam("id") String id, @RequestParam("name") String name){
        return "pong!中文+" + id + name ;
    }

测试

@RunWith(SpringRunner.class)
@SpringBootTest
class JmpApplicationTests {

    @Autowired
    private JMPService jmpService;
    @Autowired
    private RestTemplate restTemplate;

    @Test
    void testRestTemplate() {
        // 1.无参get
        String body = restTemplate.getForObject("http://192.168.1.165:8081/jmp/ping", String.class);
        // 2.1有参get restful风格 http://192.168.1.165:8081/jmp/ping/{id}
        String body = restTemplate.getForObject("http://192.168.1.165:8081/jmp/ping/{id}", String.class, 1);
        // 2.2有参get 非restful ?id={id}&name={name}
        String body = restTemplate.getForObject("http://192.168.1.165:8081/jmp/ping?id={id}&name={name}", String.class, 1, "有参get请求");
        // 2.3 用getForEntity
        ResponseEntity<String> entity = restTemplate.getForEntity("http://192.168.1.165:8081/jmp/ping?id={id}&name={name}", String.class, 1, "有参get请求");
        System.out.println( entity.getStatusCodeValue() +  entity.getBody());
        // 2.4 用map封装参数
        Map<String, String> param = new HashMap<String, String>();
        param.put("id", "1");
        param.put("name", "changeDetectionTest");
        param.put("img_1", "http://192.168.1.173:8081/guanweidata/change/2021-04-28/1-210428084858531.png");
        param.put("img_2", "http://192.168.1.173:8081/guanweidata/change/2021-04-28/2-210428084902423.png");
        param.put("detecttype", "transformation_detection");
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://localhost:8081/jmp/ping?id={id}&name={name}&img_1={img_1}&img_2={img_2}&detecttype={detecttype}", String.class, param);
        // 不能用Object接收结果,会报错
//        ResponseEntity<Object> forEntity = restTemplate.getForEntity("http://localhost:8081/jmp/ping?id={id}&name={name}&img_1={img_1}&img_2={img_2}&detecttype={detecttype}", Object.class, param);
        System.out.println(forEntity.getBody());
    }
}    
上一篇:Spring Cloud Alibaba - RestTemplate


下一篇:RestTemplate入门