• 002-06-RestTemplate-请求示例-form、json、multipart、okhttp3


    一、概述

      请求示例集合

    服务端:https://github.com/bjlhx15/common-study.git 中的 http-client-webserver

    服务端:RequestBody接收 即为Content-Type:appliaction/json接收

    服务端:RequestParam 参数映射

    1.1、form表单

    请求头:Content-Type:application/x-www-form-urlencoded

    请求方式:post

    测试:  

      

    小结:

      参数传递方式一、使用:MultiValueMap<String, String> valueMap = new LinkedMultiValueMap();

      参数传递方式二、使用:url传递普通参数、对象接收

      直接只使用对象,如没显示设置请求头,会将Content-Type设置为:application/json;

        如果显示设置,headers.add("Content-Type","application/x-www-form-urlencoded");

            Person person = new Person();
            person.setName("test");
            person.setAge(23);
    
            HttpHeaders headers=new HttpHeaders();
            headers.add("Content-Type","application/x-www-form-urlencoded");
            HttpEntity entity=new HttpEntity(person,headers);  

        会出现如下异常

    No HttpMessageConverter for [com.github.bjlhx15.common.http.webserver.controller.Person] and content type [application/x-www-form-urlencoded]

    1.2、form表单

    请求头:Content-Type:application/json

    请求方式:post

    测试:  

      

    小结:

      参数传递方式一、使用:对象类型参数,服务端需要RequestBody接收

      参数传递方式二、使用:url传递普通参数、对象接收

      直接只使用对象,如没显示设置请求头,会将Content-Type默认设置为:application/json;

    1.3、multi-part form表单

    请求头:Content-Type:multipart/form-data

    请求方式:post

    测试:  

      

    小结:

      参数传递方式一、使用:MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<>();可以传递普通参数和文件

      参数传递方式二、使用:url传递普通参数、对象接收

        @Test
        public void uploadFileParam() {
            RestTemplate restTemplate2 = new RestTemplate(new SimpleClientHttpRequestFactory());
            //设置请求头
            HttpHeaders headers1 = new HttpHeaders();
            headers1.setContentType(MediaType.MULTIPART_FORM_DATA);
    
            MultiValueMap<String, Object> valueMap = new LinkedMultiValueMap<>();
            FileSystemResource fileSystemResource = new FileSystemResource(new File("/Users/lihongxu6/IdeaProjects/common-study/common-http/http-client-test/target/test-classes/test.txt"));
            valueMap.add("file", fileSystemResource);
            valueMap.add("msg", "dddddd");
            HttpEntity entity = new HttpEntity(valueMap, headers1);
    
            Result result2 = restTemplate2.postForObject("http://localhost:8080/multipart/uploadFileParam", entity, Result.class);
            System.out.println(result2);
        }

    针对特殊服务端可能请求无法接收普通参数,因为传递过程对普通参数增加了Content-Type。

    故可以使用okhttp3,如下

    @PostMapping("upload")
    public WebResult<ImageUploadResult> upload(@RequestParam("img") MultipartFile file) {
        final byte[] bytes;
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            log.warn("fail to read file", file.getOriginalFilename(), e);
            return new WebResult<>(500, "服务器错误,请稍后再试");
        }
        
        //构造请求Body
        MultipartBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("name", "bjlhx15")
                .addFormDataPart("file", bytes))
                .build();
     
        //构造请求
        Request request = new Request.Builder().url("https://aaa.com/upload")
                .post(body)
                .build();
        OkHttpClient client = new OkHttpClient.Builder()
                .sslSocketFactory(sslManager.getSocketFactory())
                .build();
        //后面的省略了...
    }

    更换

  • 相关阅读:
    Tensorflow源码解析2 -- 前后端连接的桥梁
    Tensorflow源码解析1 -- 内核架构和源码结构
    Python保存json文件并格式化
    如何在没有https环境下使用webrtc
    github提交代码不用输入账号密码的解决方案
    使用nodeJs在本地搭建最简单的服务
    ubuntu16.04 安装 nginx 服务器
    git pull和git pull --rebase的使用
    Linux 下各个目录的作用及内容
    阿里云服务器(Ubuntu16.04 64位)远程连接
  • 原文地址:https://www.cnblogs.com/bjlhx/p/11285974.html
Copyright © 2020-2023  润新知