Java调用HTTP接口及返回json数据讲解

本文主要为了总结一下Java常用基础中关于http接口调用以及前后端json数据的返回方式,案例如下,如有误区或不足,还请大佬多多指教。话不多说,直接码

一,Java调用http接口

在我们开发代码的过程中常常会涉及到调用其他接口或数据,下面便简单总结一下关于调用http接口的方法:

当前开发中主要流行及通用的有四种:

1、通过JDK网络类Java.net.HttpURLConnection;

2、通过common封装好的HttpClient;

3、通过Apache封装好的CloseableHttpClient;

4、通过SpringBoot-RestTemplate;

【我们简单一一介绍一下】

1.1 HttpURLConnection

方法一(post):

private static void httpURLPOSTCase() {
        String methodUrl = "http://xxx.xxx.xx.xx:8280/xx/adviserxx ";
        HttpURLConnection connection = null;
        OutputStream dataout = null;
        BufferedReader reader = null;
        String line = null;
        try {
            URL url = new URL(methodUrl);
            connection = (HttpURLConnection) url.openConnection();// 根据URL生成HttpURLConnection
            connection.setDoOutput(true);// 设置是否向connection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true,默认情况下是false
            connection.setDoInput(true); // 设置是否从connection读入,默认情况下是true;
            connection.setRequestMethod("POST");// 设置请求方式为post,默认GET请求
            connection.setUseCaches(false);// post请求不能使用缓存设为false
            connection.setConnectTimeout(3000);// 连接主机的超时时间
            connection.setReadTimeout(3000);// 从主机读取数据的超时时间
            connection.setInstanceFollowRedirects(true);// 设置该HttpURLConnection实例是否自动执行重定向
            connection.setRequestProperty("connection", "Keep-Alive");// 连接复用
            connection.setRequestProperty("charset", "utf-8");

            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Authorization", "Bearer 66cb225f1c3ff0ddfdae31rae2b57488aadfb8b5e7");
            connection.connect();// 建立TCP连接,getOutputStream会隐含的进行connect,所以此处可以不要

            dataout = new DataOutputStream(connection.getOutputStream());// 创建输入输出流,用于往连接里面输出携带的参数
            String body = "[{\"orderNo\":\"44921902\",\"adviser\":\"测试\"}]";
            dataout.write(body.getBytes());
            dataout.flush();
            dataout.close();
     //该部分也可传递键值对参数(同上述)
     // String body = "userName=zhangsan&password=123456";
            //BufferedWriter writer = new BufferedWriter(new //OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
           // writer.write(body);
           // writer.close();

            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));// 发送http请求
                StringBuilder result = new StringBuilder();
                // 循环读取流
                while ((line = reader.readLine()) != null) {
                    result.append(line).append(System.getProperty("line.separator"));//
                }
                System.out.println(result.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            connection.disconnect();
        }
    }

方法二(get):

private void httpURLGETCase() {
        String methodUrl = "http://xx.xx.xx.xx:8086/sp-test/usertest/query";
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        String line = null;
        try {
        //在该处可添加所需参数
            URL url = new URL(methodUrl + "?mobile=15334567890&name=zhansan");
            connection = (HttpURLConnection) url.openConnection();// 根据URL生成HttpURLConnection
            connection.setRequestMethod("GET");// 默认GET请求
            connection.connect();// 建立TCP连接
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));// 发送http请求
                StringBuilder result = new StringBuilder();
                // 循环读取流
                while ((line = reader.readLine()) != null) {
                    result.append(line).append(System.getProperty("line.separator"));// "\n"
                }
                System.out.println(result.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            connection.disconnect();
        }
    }
2.1 HttpClient

首先,导入于下包:



    commons-httpclient
    commons-httpclient
    3.1




    com.alibaba
    fastjson
    1.2.32

方法一(get):

public static String doGet(String url, String charset) {
    //1.生成HttpClient对象并设置参数
    HttpClient httpClient = new HttpClient();
    //设置Http连接超时为5秒
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
    //2.生成GetMethod对象并设置参数
    GetMethod getMethod = new GetMethod(url);
    //设置get请求超时为5秒
    getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
    //设置请求重试处理,用的是默认的重试处理:请求三次
    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
    String response = "";
    //3.执行HTTP GET 请求
    try {
        int statusCode = httpClient.executeMethod(getMethod);
        //4.判断访问的状态码
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("请求出错:" + getMethod.getStatusLine());
        }
        //5.处理HTTP响应内容
        //HTTP响应头部信息,这里简单打印
        Header[] headers = getMethod.getResponseHeaders();
        for(Header h : headers) {
            System.out.println(h.getName() + "---------------" + h.getValue());
        }
        //读取HTTP响应内容,这里简单打印网页内容
        //读取为字节数组
        byte[] responseBody = getMethod.getResponseBody();
        response = new String(responseBody, charset);
        System.out.println("-----------response:" + response);
        //读取为InputStream,在网页内容数据量大时候推荐使用
        //InputStream response = getMethod.getResponseBodyAsStream();
    } catch (HttpException e) {
        //发生致命的异常,可能是协议不对或者返回的内容有问题
        System.out.println("请检查输入的URL!");
        e.printStackTrace();
    } catch (IOException e) {
        //发生网络异常
        System.out.println("发生网络异常!");
    } finally {
        //6.释放连接
        getMethod.releaseConnection();
    }
    return response;
}

方法二(post):

public static String doPost(String url, JSONObject json){
    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(url);

    postMethod.addRequestHeader("accept", "*/*");
    postMethod.addRequestHeader("connection", "Keep-Alive");
    //设置json格式传送
    postMethod.addRequestHeader("Content-Type", "application/json;charset=GBK");
    //必须设置下面这个Header
    postMethod.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
    //添加请求参数
    postMethod.addParameter("commentId", json.getString("commentId"));

    String res = "";
    try {
        int code = httpClient.executeMethod(postMethod);
        if (code == 200){
            res = postMethod.getResponseBodyAsString();
            System.out.println(res);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return res;
}
3.1 CloseableHttpClient

CloseableHttpClient是在HttpClient的基础上修改更新而来的,这里还涉及到请求头token的设置(请求验证),利用fastjson转换请求或返回结果字符串为json格式,当然上面两种方式也是可以设置请求头token、json的,这里只在下面说明

导入下面部分



    org.apache.httpcomponents
    httpclient
    4.5.2




    com.alibaba
    fastjson
    1.2.32

该部分也分为get及post,话不多说,直接码:

package com.riemann.springbootdemo.util.common.httpConnectionUtil;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author riemann
 * @date 2019/05/25 1:35
 */
public class CloseableHttpClientUtil {

    private static String tokenString = "";
    private static String AUTH_TOKEN_EXPIRED = "AUTH_TOKEN_EXPIRED";
    private static CloseableHttpClient httpClient = null;

    /**
     * 以get方式调用第三方接口
     * @param url
     * @param token
     * @return
     */
    public static String doGet(String url, String token) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);
        if (null != tokenString && !tokenString.equals("")) {
            tokenString = getToken();
        }
        //api_gateway_auth_token自定义header头,用于token验证使用
        httpGet.addHeader("api_gateway_auth_token",tokenString);
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 以post方式调用第三方接口
     * @param url
     * @param json
     * @return
     */
    public static String doPost(String url, JSONObject json) {
        if (null == httpClient) {
            httpClient = HttpClientBuilder.create().build();
        }
        HttpPost httpPost = new HttpPost(url);
        if (null != tokenString && tokenString.equals("")) {
            tokenString = getToken();
        }
        //api_gateway_auth_token自定义header头,用于token验证使用
        httpPost.addHeader("api_gateway_auth_token", tokenString);
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {
            StringEntity se = new StringEntity(json.toString());
            se.setContentEncoding("UTF-8");
            //发送json数据需要设置contentType
            se.setContentType("application/x-www-form-urlencoded");
            //设置请求参数
            httpPost.setEntity(se);
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 获取第三方接口的token
     */
    public static String getToken() {
        String token = "";
        JSONObject object = new JSONObject();
        object.put("appid", "appid");
        object.put("secretkey", "secretkey");
        if (null == httpClient) {
            httpClient = HttpClientBuilder.create().build();
        }
        HttpPost httpPost = new HttpPost("http://localhost/login");
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {
            StringEntity se = new StringEntity(object.toString());
            se.setContentEncoding("UTF-8");
            //发送json数据需要设置contentType
            se.setContentType("application/x-www-form-urlencoded");
            //设置请求参数
            httpPost.setEntity(se);
            HttpResponse response = httpClient.execute(httpPost);
            //这里可以把返回的结果按照自定义的返回数据结果,把string转换成自定义类
            //ResultTokenBO result = JSONObject.parseObject(response, ResultTokenBO.class);
            //把response转为jsonObject
            JSONObject result = (JSONObject) JSONObject.parseObject(String.valueOf(response));
            if (result.containsKey("token")) {
                token = result.getString("token");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return token;
    }

    /**
     * 测试
     */
    public static void test(String telephone) {

        JSONObject object = new JSONObject();
        object.put("telephone", telephone);

        //首先获取token
        tokenString = getToken();
        String response = doPost("http://localhost/searchUrl", object);
        //如果返回的结果是list形式的,需要使用JSONObject.parseArray转换
        //List list = JSONObject.parseArray(response, Result.class);
        System.out.println(response);
    }

    public static void main(String[] args) {
        test("12345678910");
    }
}
4.1 SpringBoot-RestTemplate

springBoot-RestTemple是上面三种方式的集大成者,代码编写更加简单

导入于下:


        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
    

    
        
        
            org.apache.httpcomponents
            httpclient
            4.5.2
        

        
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        
        
            org.springframework.boot
            spring-boot-starter-jetty
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

在启动类下创建RestTemplateConfig.java类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * @author riemann
 * @date 2019/05/25 2:16
 */
@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(15000);
        factory.setReadTimeout(5000);
        return factory;
    }
}

正式代码如下,个人感觉比较简洁:

import com.alibaba.fastjson.JSONObject;
import com.swordfall.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author riemann
 * @date 2019/05/25 2:20
 */
@Service
public class RestTemplateToInterface {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 以get方式请求第三方http接口 getForEntity
     * @param url
     * @return
     */
    public User doGetWith1(String url){
        ResponseEntity responseEntity = restTemplate.getForEntity(url, User.class);
        User user = responseEntity.getBody();
        return user;
    }

    /**
     * 以get方式请求第三方http接口 getForObject
     * 返回值返回的是响应体,省去了我们再去getBody()
     * @param url
     * @return
     */
    public User doGetWith2(String url){
        User user  = restTemplate.getForObject(url, User.class);
        return user;
    }

    /**
     * 以post方式请求第三方http接口 postForEntity
     * @param url
     * @return
     */
    public String doPostWith1(String url){
        User user = new User("小白", 20);
        ResponseEntity responseEntity = restTemplate.postForEntity(url, user, String.class);
        String body = responseEntity.getBody();
        return body;
    }

    /**
     * 以post方式请求第三方http接口 postForEntity
     * @param url
     * @return
     */
    public String doPostWith2(String url){
        User user = new User("小白", 20);
        String body = restTemplate.postForObject(url, user, String.class);
        return body;
    }

    /**
     * exchange
     * @return
     */
    public String doExchange(String url, Integer age, String name){
        //header参数
        HttpHeaders headers = new HttpHeaders();
        String token = "asdfaf2322";
        headers.add("authorization", token);
        headers.setContentType(MediaType.APPLICATION_JSON);

        //放入body中的json参数
        JSONObject obj = new JSONObject();
        obj.put("age", age);
        obj.put("name", name);

        //组装
        HttpEntity request = new HttpEntity<>(obj, headers);
        ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
        String body = responseEntity.getBody();
        return body;
    }
}

二,Java返回json数据

1,自写代码

1.1 后端部分
public string xxx { HttpHttpServletRequest request,HttpServletResponse response} 
{     JSONObject json =new JSONObject();//定义相对应的json对象
      json.put("result"," success")
      response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
      PrintWriter out = null;
      out = response.getWriter();
      out.write(json.toString()); 
}
JSONObject所必需的6个jar包(版本自定义):

commons-beanutils-1.7.0.jar

commons-collections-3.1.jar

commons-lang-2.5.jar

commons-logging.jar

ezmorph-1.0.3.jar

json-lib-2.1-jdk15.jar
1.2前端部分
$.ajax({
data : {
// userNameOrTel: $("#user").val(),
// password: $("#pwd").val()
},
type : "post",  
url : "admin/login/",
dataType : "json",
contentType : "application/json;charset=utf-8", 
async : false,  //同步 异步
success : function(data) {
    debugger; 
}
}
});

2,第二种使用JSON工具将对象序列化成json,常用工具Jackson,fastjson,gson。

利用HttpServletResponse,然后获取response.getOutputStream()或response.getWriter() 直接输出。

2.1 后端部分
public class JsonUtil  
{  
      
    private static Gson gson=new Gson();  
  
  
    /** 
     * @MethodName : toJson 
     * @Description : 将对象转为JSON串,此方法能够满足大部分需求 
     * @param src 
     *            :将要被转化的对象 
     * @return :转化后的JSON串 
     */  
    public static String toJson(Object src) {  
        if (src == null) {  
            return gson.toJson(JsonNull.INSTANCE);  
        }  
        return gson.toJson(src);  
    }  
}  
2.2 前端部分
该部分同上1.2部分

3,通过spring mvc3的注解实现(@Restcontroller| | @Responsebody)

3.1 后端部分
@ResponseBody  
  @RequestMapping("/list")  
  public List list(ModelMap modelMap) {  
    String hql = "select c from Clothing c ";  
    Page page = new Page();  
    page.setPageSize(6);  
    page  = clothingServiceImpl.queryForPageByHql(page, hql);  
      
    return page.getResult();  
  }  
3.2 前端部分

①@Responsebody部分:

$.ajax({
data : {
// userNameOrTel: $("#name").val(),
// password: $("#pwd").val()
},
type : "post",
url : "haha",
dataType : "json",
//contentType : "application/json;charset=utf-8", // 区别在这里,不要加,不然接收不到请求参数
async : false,   //同步异步
success : function(msg) {
debugger;}}});

②@Restcontroller

data:JSON.stringify({'channelId':channelId}),
                  success:function(data){
                        alert(data.channelId);
                  },
 
contentType:'application/json;charset=utf-8'

感谢各位大佬的关注和支持,代码偏旧,如有不好,请勿喷,谢谢,如有需要请转载。

本文部分代码参考与:https://blog.csdn.net/riemann_/article/details/90539829
https://www.cnblogs.com/zjshd/p/9149643.html
https://blog.csdn.net/qq_26289533/article/details/78749057

你可能感兴趣的:(Java,java,http,json)