springboot restTemplate 使用示例


springboot restTemplate 使用示例

 

 

**************************

示例

 

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello world";
    }

    @RequestMapping("/hello2/{name}")
    public String hello2(@PathVariable("name") String name){
        return name;
    }

    @RequestMapping("/hello3")
    public Person hello3(String name,Integer age){
        Person person=new Person();
        person.setName(name);
        person.setAge(age);

        return person;
    }

    @RequestMapping("/hello4")
    public Person hello4(@RequestBody Person person){
        return person;
    }

    @RequestMapping("/hello5")
    public String hello5(HttpServletRequest request){
        Enumeration<String> headerNames=request.getHeaderNames();

        while (headerNames.hasMoreElements()){
            String name=headerNames.nextElement();
            System.out.println(name+" ==> "+request.getHeader(name));
        }

        return "success";
    }
}

 

Hello2Controller

@RestController
public class Hello2Controller {

    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/test")
    public String test(){
        return restTemplate.getForObject("http://localhost:8080/hello",String.class);
    }

    @RequestMapping("/test2")
    public String test2(){
        return restTemplate.getForObject("http://localhost:8080/hello2/{name}",String.class,"瓜田李下");
    }

    @RequestMapping("/test3")
    public String test3(){
        Map<String,String> params=new HashMap<>();
        params.put("name","瓜田李下");

        return restTemplate.getForObject("http://localhost:8080/hello2/{name}",String.class,params);
    }

    @RequestMapping("/test4")
    public Person test4(){
        Map<String,String> params=new HashMap<>();
        params.put("name","瓜田李下");
        params.put("age","20");

        return restTemplate.getForObject("http://localhost:8080/hello3?name={name}&age={age}",Person.class,params);
    }

    @RequestMapping("/test5")
    public String test5(){
        return restTemplate.postForObject("http://localhost:8080/hello2/{name}",null,String.class,"瓜田李下");
    }

    @RequestMapping("/test6")
    public String test6(){
        Map<String,String> params=new HashMap<>();
        params.put("name","瓜田李下");

        return restTemplate.postForObject("http://localhost:8080/hello2/{name}",null,String.class,params);
    }

    @RequestMapping("/test7")
    public Person test7(){
        Map<String,String> params=new HashMap<>();
        params.put("name","瓜田李下");
        params.put("age","20");

        return restTemplate.postForObject("http://localhost:8080/hello3?name={name}&age={age}",null,Person.class,params);
    }

    @RequestMapping("/test8")
    public Person test8(){
        Map<String,String> params=new HashMap<>();
        params.put("name","瓜田李下");
        params.put("age","20");

        HttpEntity<Map<String,String>> entity=new HttpEntity<>(params);

        return restTemplate.postForObject("http://localhost:8080/hello4",entity,Person.class);
    }

    @RequestMapping("/test9")
    public String test9(){
        HttpHeaders headers=new HttpHeaders();
        headers.add("custom","custom header");

        HttpEntity<Object> httpEntity=new HttpEntity<>(headers);

        return restTemplate.postForObject("http://localhost:8080/hello5",httpEntity,String.class);
    }
}

 

 

**************************

使用测试

 

localhost:8080/test,输出:hello world

localhost:8080/test2,输出:瓜田李下

localhost:8080/test3,输出:瓜田李下

 

localhost:8080/test4,输出:

                        

 

localhost:8080/test5,输出:瓜田李下

localhost:8080/test6,输出:瓜田李下

 

localhost:8080/test7,输出:

                        

 

localhost:8080/test8,输出

                        

                       

localhost:8080/test9,控制台输出:

accept ==> text/plain, application/json, application/*+json, */*
custom ==> custom header
user-agent ==> Java/14.0.1
host ==> localhost:8080
connection ==> keep-alive
content-type ==> application/x-www-form-urlencoded
content-length ==> 0

自定义header添加成功

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值