100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Java调用HTTP接口及返回json数据讲解

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

时间:2022-11-25 00:41:08

相关推荐

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

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

一,Java调用http接口

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

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

1、通过JDK网络类.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生成HttpURLConnectionconnection.setDoOutput(true);// 设置是否向connection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true,默认情况下是falseconnection.setDoInput(true); // 设置是否从connection读入,默认情况下是true;connection.setRequestMethod("POST");// 设置请求方式为post,默认GET请求connection.setUseCaches(false);// post请求不能使用缓存设为falseconnection.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生成HttpURLConnectionconnection.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

首先,导入于下包:

<!--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>

方法一(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");//必须设置下面这个HeaderpostMethod.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的,这里只在下面说明

导入下面部分

<!--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>

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

package com.riemann.mon.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 /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数据需要设置contentTypese.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数据需要设置contentTypese.setContentType("application/x-www-form-urlencoded");//设置请求参数httpPost.setEntity(se);HttpResponse response = httpClient.execute(httpPost);//这里可以把返回的结果按照自定义的返回数据结果,把string转换成自定义类//ResultTokenBO result = JSONObject.parseObject(response, ResultTokenBO.class);//把response转为jsonObjectJSONObject 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);//首先获取tokentokenString = 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是上面三种方式的集大成者,代码编写更加简单

导入于下:

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

在启动类下创建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 /05/25 2:16*/@Configurationpublic class RestTemplateConfig {@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory factory){return new RestTemplate(factory);}@Beanpublic 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 /05/25 2:20*/@Servicepublic class RestTemplateToInterface {@Autowiredprivate 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;}}

二,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.jarcommons-collections-3.1.jarcommons-lang-2.5.jarcommons-logging.jarezmorph-1.0.3.jarjson-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<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 前端部分

①@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'

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

本文部分代码参考与:/riemann_/article/details/90539829

/zjshd/p/9149643.html

/qq_26289533/article/details/78749057

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。