Spring Boot RestTemplate

REST in Spring 3: RestTemplate
*
*
*
webClient:是Spring-webFlux包下的,非阻塞响应,最低java8支持函数式编程,性能好;【推荐】
RestTemplate:是Spring-webmvc包下的,是阻塞响应,满足RestFul原则,代码简单,默认依赖jdk的HTTP连接工具。【少用】
HttpClient:是apache httpClient包下的,代码复杂,需要资源回收。【不用】
OkHttp:

RestTemplateConfig.java

package com.example.demofastjson.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(){
        // RestTemplate restTemplate = new RestTemplate();
        //设置中文乱码问题方式一
        // restTemplate.getMessageConverters().add(1,new StringHttpMessageConverter(Charset.forName("UTF-8")));
        // 设置中文乱码问题方式二
        // restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 支持中文编码
        return new RestTemplate();
    }
}

RestTemplateController.java

package com.example.demofastjson.controller;

import com.example.demofastjson.pojo.UserRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("resttemp")
public class RestTemplateController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * ok
     * @return
     */
    @GetMapping("find")
    public ResponseEntity<String> findByPage(){
        ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:36247/weatherforecast/string", String.class);
        System.out.println("状态码:"+entity.getStatusCode());
        System.out.println("响应体"+entity.getBody());
        return ResponseEntity.ok(entity.getBody());
    }

    /**
     * ok
     * @return
     */
    @RequestMapping("get1")
    public String Get1() {
        String response = restTemplate.getForObject("http://localhost:36247/weatherforecast/string",String.class);
        return response;
    }

    /**
     * ok
     * HashMap 参数
     * @return
     */
    @RequestMapping("add")
    public String add() {
        //接口地址
        String url = "http://localhost:36247/weatherforecast";

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization","123");
        MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);

        HashMap<String, Object> map = new HashMap<>();
        map.put("name","admin");
        map.put("password","123456");

        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map,headers);

        ResponseEntity<String> responseResultResponseEntity = restTemplate.postForEntity(url, objectHttpEntity, String.class);
        return "123";
    }

    /**
     * ok
     * 请求的接口参数是FromBody,不能使用 MultiValueMap
     * @return
     */
    @RequestMapping("create")
    public String create() {
        //接口地址
        String url = "http://localhost:36247/weatherforecast";

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization","123");
        MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);

        /**
         * 请求的接口参数是FromBody,此处使用 MultiValueMap 会报错
         * MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
         * paramMap.add("name","admin");
         * paramMap.add("password","123456");
         */

        UserRequest request = new UserRequest();
        request.setName("admin");
        request.setPassword("123456");

        HttpEntity<UserRequest> objectHttpEntity = new HttpEntity<>(request,headers);

        ResponseEntity<String> responseResultResponseEntity = restTemplate.postForEntity(url, objectHttpEntity, String.class);
        return "123 " + responseResultResponseEntity.getBody();
    }

    /**
     * ok
     * HashMap 参数
     * @return
     */
    @RequestMapping("exchange")
    public String exchange() {
        //接口地址
        String url = "http://localhost:36247/weatherforecast";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //headers.add("Content-Type","application/json");
        headers.add("Authorization","123");

        HashMap<String, Object> map = new HashMap<>();
        map.put("name","admin_create");
        map.put("password","123456_create");

        HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map,headers);

        ResponseEntity<String> responseResultResponseEntity = restTemplate.exchange(url, HttpMethod.POST, objectHttpEntity, String.class);

        return "123";
    }

    /**
     * ok
     * @return
     */
    @RequestMapping("delete")
    public ResponseEntity<String> deleteGoods(){
        restTemplate.delete("http://localhost:36247/weatherforecast",String.class);
        return ResponseEntity.ok("删除成功");
    }

    @RequestMapping("get3")
    public String get3() {
        String url = "http://localhost:4365/weatherforecast/c?name={name}&password={password}";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("Authorization","123");

        /**
         * ok
         * get请求,string Get(string name,string password)    能获取到参数
         * get请求,string Get([FromForm] UserRequest request) 不能获取到参数
         */
        MultiValueMap<String, Object> paramMap1 = new LinkedMultiValueMap<>();
        paramMap1.add("name","admin");
        paramMap1.add("password","123456");

        /**
         * ok
         * get请求,string Get(string name,string password)    能获取到参数
         * get请求,string Get([FromForm] UserRequest request) 不能获取到参数
         */
        Map<String, Object> paramMap2 = new HashMap<>();
        paramMap2.put("name", "20181116");
        paramMap2.put("password", "10");

        /**
         * ok
         * get请求,string Get(string name,string password)    能获取到参数
         * get请求,string Get([FromForm] UserRequest request) 不能获取到参数
         */
        Map paramMap3 = new HashMap();
        paramMap3.put("name","4d6ab733dcfed0e82806b9a97ff602ff");
        paramMap3.put("password","330100");

        /**
         * ok
         * get请求,string Get(string name,string password)    能获取到参数
         * get请求,string Get([FromForm] UserRequest request) 不能获取到参数
         */
        Map<String,String> paramMap4=new HashMap<>();
        paramMap4.put("name","DFCFMonitor");
        paramMap4.put("password","123123123123");

        restTemplate.getForEntity(url,String.class,paramMap4);
        return "123";
    }
}

*
*
*

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值