由于请求数据接口是跨域的,但是我们无法改变接口的代码
先从请求后台,然后从后台进行二次请求,请求数据接口
原生代码
package edu.nf.http.test;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpUriRequest;import org.apache.http.client.methods.RequestBuilder;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import org.junit.Test;import java.io.IOException;/** * @Author Eric * @Date 2018/12/10 */public class HttpClientTest { @Test public void testGet(){ //创建一个HttpClient对象,用于发送请求和接收响应信息 CloseableHttpClient client = HttpClients.createDefault(); //创建请求构建起(工厂) RequestBuilder builder = RequestBuilder.get(); //设置请求的uri builder.setUri("http://c.3g.163.com/nc/article/list/T1348647853363/0-40.html"); //创建一个get请求对象 HttpUriRequest request = builder.build(); //发送请求,并返回响应对象 try { CloseableHttpResponse response = client.execute(request); //从响应对象中获取数据,这里使用了一个HttpEntity封装 HttpEntity entity = response.getEntity(); //从Entity中获取真正的响应内容 String json = EntityUtils.toString(entity); System.out.println(json); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } } @Test public void testPost(){ //创建一个HttpClient对象,用于发送请求和接收响应信息 CloseableHttpClient client = HttpClients.createDefault(); RequestBuilder builder = RequestBuilder.post(); builder.setUri("http://c.3g.163.com/nc/article/list/T1348647853363/0-40.html"); //可以提交参数 builder.addParameter("user","admin").addParameter("age","18"); //构建请求对象 HttpUriRequest request = builder.build(); //发送请求 try { CloseableHttpResponse response = client.execute(request); //获取响应数据 HttpEntity entity = response.getEntity(); String json = EntityUtils.toString(entity); System.out.println(json); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } }}
封装成工具类
package edu.nf.http.conmmons;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpUriRequest;import org.apache.http.client.methods.RequestBuilder;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.io.IOException;import java.util.Map;/** * @Author Eric * @Date 2018/12/10 * 封装工具类 */public class HttpUtils { /** * 创建一个唯一的client对象 */ private static CloseableHttpClient client = HttpClients.createDefault(); /** * 发送get请求 * @param uri * @return */ public static String get(String uri){ //构建get请求 HttpUriRequest request = RequestBuilder.get(uri).build(); return execute(request); } /** * 发送post请求 * @param uri * @param params post所提交的参数 * @return */ public static String post(String uri, Mapparams){ RequestBuilder builder = RequestBuilder.post(uri); //循环设置参数 for (String name : params.keySet()) { builder.addParameter(name,params.get(name)); } HttpUriRequest request = builder.build(); return execute(request); } /** * 发送请求并返回响应内容 * @param request * @return */ private static String execute(HttpUriRequest request){ try { //发送请求 CloseableHttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } }}
json序列化工具类
(获取到的数据是字符串,序列化成json)
package edu.nf.http.conmmons;import com.fasterxml.jackson.databind.ObjectMapper;import java.io.IOException;/** * @Author Eric * @Date 2018/12/10 */public class JsonUtils { public staticT convert(String json,Class clazz){ ObjectMapper mapper = new ObjectMapper(); try { return mapper.readValue(json,clazz); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } }}
Service过滤
package edu.nf.http.service;import edu.nf.http.conmmons.HttpUtils;import edu.nf.http.conmmons.JsonUtils;import org.springframework.stereotype.Service;import java.util.List;import java.util.Map;/** * @Author Eric * @Date 2018/12/10 */@Servicepublic class NewsService { public List
ResponseVO
package edu.nf.http.controller.vo;/** * @Author Eric * @Date 2018/12/6 */public class ResponseVO{ private Integer code; private String message; private T data; @Override public String toString() { return "ResponseVO{" + "code=" + code + ", message='" + message + '\'' + ", data=" + data + '}'; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; }}
Controller
package edu.nf.http.controller;import edu.nf.http.controller.vo.ResponseVO;import edu.nf.http.service.NewsService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.Map;/** * @Author Eric * @Date 2018/12/10 */@RestControllerpublic class NewsController { @Autowired private NewsService newsService; @GetMapping("/listNews") public ResponseVO listNews(){ ResponseVO vo = new ResponseVO(); List> list = newsService.listNews(); vo.setCode(HttpStatus.OK.value()); vo.setMessage("无数据"); vo.setData(list); return vo; }}
HTML
(使用Postman工具进行请求可以查看返回的字段,在页面可以用对象进行输出)
新闻头条 今日头条
简单列表
标题子标题