当前位置:   article > 正文

Java post 表单请求和json请求第三方接口_java class clazz

java class clazz
1、表单请求
public final <Q, R> R getRequest(HttpExecutor httpExecutor, String uri, Class<R> clazz) {
        R response = null;
        List<NameValuePair> headers = new ArrayList<NameValuePair>();

        headers.add(new BasicNameValuePair("Accept", "*/*"));
        headers.add(new BasicNameValuePair("Accept-Encoding", "gzip, deflate"));
        headers.add(new BasicNameValuePair("Accept-Language", "zh-CN,zh;q=0.8,en-us;q=0.5,en;q=0.3,*;q=0.2"));
        headers.add(new BasicNameValuePair("Connection", "keep-alive"));
        headers.add(new BasicNameValuePair("Cache-Control", "max-age=0"));
        headers.add(new BasicNameValuePair("Pragma", "no-cache"));
        headers.add(new BasicNameValuePair("User-Agent", "Mozilla/5.0"));
        headers.add(new BasicNameValuePair("clientCode", clientCode));
        headers.add(new BasicNameValuePair("tradeCode", tradeCode));
        headers.add(new BasicNameValuePair("requestId", UUID.randomUUID().toString()));

        try {
            HttpExecutor.Response resp = httpExecutor.executeGet(uri, headers);

            if (resp.isSuccess()) {
                String result = resp.toString();
                if (clazz.equals(String.class)) {
                    response = (R) result;
                } else {
                    response = JSON.parseObject(result, clazz);
                }

            } else if (!resp.isNoError()) {
                throw new ServiceException(resp.getErrorInfo(), NETWORK_ERROR);
            } else {
                throw new IllegalStateException(String.format("unexpected status code '%d'", resp.statusCode));
            }
        } catch (Exception e) {
            throw wrapServiceException(e);
        }

        return response;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
2、json请求
  /**
     * 组装请求数据
     *
     * @param httpExecutor
     * @param uri
     * @param requestBody
     * @param token
     * @return
     */
    public final String getRequest(HttpExecutor httpExecutor, String uri, String requestBody, String token) {
        List<NameValuePair> headers = new ArrayList<>();
        headers.add(new BasicNameValuePair("Content-Type", "application/json"));
        if (StringUtils.isNotEmpty(token)) {
            headers.add(new BasicNameValuePair("token", token));
        }
        try {
            log.info("CallServiceImpl.getRequest headers:{}", JSON.toJSONString(headers));
            HttpExecutor.Response resp = httpExecutor.executeStringPost(uri, requestBody, null, headers);
            if (resp.isSuccess()) {
                return resp.toString();
            } else if (!resp.isNoError()) {
                throw new ServiceException(resp.getErrorInfo(), NETWORK_ERROR);
            } else {
                throw new IllegalStateException(String.format("unexpected status code '%d'", resp.statusCode));
            }
        } catch (Exception e) {
            throw wrapServiceException(e);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

不同的是请求体的封装方式。

3、区别
  • 1 表单提交

(1)从前端传过来的请求参数是key=value形式的

(2)springmvc自动进行参数的绑定

  • 2 json格式提交

(1)前端传过来的参数是字符串,以json格式呈现

(2)springmvc接收需要使用@RequestBody注解,对json字符串进行解析

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

闽ICP备14008679号