Explanation of Java calling HTTP interface and returning json data

This article is mainly to summarize the http interface call and the return method of front-end and back-end json data in the common basics of Java. The cases are as follows. If there are any misunderstandings or shortcomings, please advise me. Not much to say, direct code

One, Java calls the http interface

In the process of our code development, it often involves calling other interfaces or data. Here is a brief summary of the method of calling the http interface:

There are four main popular and common ones in current development:

1. Through the JDK network class Java.net.HttpURLConnection;

2. HttpClient encapsulated by common;

3. CloseableHttpClient packaged by Apache;

4. Through SpringBoot-RestTemplate;

[We briefly introduce one by one]

1.1 HttpURLConnection

Method one (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();
        }
    }

Method two (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

First, import in the following package:

<!--HttpClient-->
<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>

<!--fastjson-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.32</version>
</dependency>

Method one (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;
}

Method two (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 is modified and updated on the basis of HttpClient. It also involves the setting of the request header token (request verification). Use fastjson to convert the request or return the result string to json format. Of course, the above two methods can also set the request header. token, json, here is only explained below

Import the following part

<!--CloseableHttpClient-->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

<!--fastjson-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.32</version>
</dependency>

This part is also divided into get and post, not much to say, direct code:

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<Result> list = JSONObject.parseArray(response, Result.class);
        System.out.println(response);
    }

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

springBoot-RestTemple is a master of the above three methods, the code writing is easier

Imported below:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencies>
        <!--CloseableHttpClient-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>

        <!--spring restTemplate-->
        <!-- @ConfigurationProperties annotation processing (metadata for IDEs)
                生成spring-configuration-metadata.json类,需要引入此类-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

Create the RestTemplateConfig.java class under the startup class

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;
    }
}

The official code is as follows, personally feel more concise:

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<User> 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<String> 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<JSONObject> request = new HttpEntity<>(obj, headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
        String body = responseEntity.getBody();
        return body;
    }
}

Second, Java returns json data

1. Self-written code

1.1 Backend
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 Front end
$.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. The second method uses JSON tools to serialize objects into json, commonly used tools Jackson, fastjson, gson.

Use HttpServletResponse, and then get response.getOutputStream() or response.getWriter() to output directly.

2.1 Backend
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 Front end
该部分同上1.2部分

3. Implemented through spring mvc3 annotations (@Restcontroller| | @Responsebody)

3.1 Backend
@ResponseBody  
  @RequestMapping("/list")  
  public List<String> list(ModelMap modelMap) {  
    String hql = "select c from Clothing c ";  
    Page<Clothing> page = new Page<Clothing>();  
    page.setPageSize(6);  
    page  = clothingServiceImpl.queryForPageByHql(page, hql);  
      
    return page.getResult();  
  }  
3.2 Front end

①@Responsebody part:

$.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'

Thank you for your attention and support, the code is too old, if it is not good, please do not spray it, thank you, please reprint if necessary.

Part of the code reference in this article: 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

Guess you like

Origin blog.csdn.net/MatChen/article/details/111311358