boot spring 接口接收数据_SpringBoot请求网页接口数据返回JSON,并存入本地数据库...
GetJson文件 public class GetJson {public JSONObject getHttpJson(String url, int comefrom) throws Exception {try {URL realUrl = new URL(url);HttpURLConnection connection = (HttpURLConnection) realUrl.ope
GetJson文件 public class GetJson {
public JSONObject getHttpJson(String url, int comefrom) throws Exception {
try {
URL realUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
//请求成功
if (connection.getResponseCode() == 200) {
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//10MB的缓存
byte[] buffer = new byte[10485760];
int len = 0;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String jsonString = baos.toString();
baos.close();
is.close();
//转换成json数据处理
// getHttpJson函数的后面的参数1,表示返回的是json数据,2表示http接口的数据在一个()中的数据
JSONObject jsonArray = getJsonString(jsonString, comefrom);
return jsonArray;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
public JSONObject getJsonString(String str, int comefrom) throws Exception{
JSONObject jo = null;
if(comefrom==1){
return new JSONObject(str);
}else if(comefrom==2){
int indexStart = 0;
//字符处理
for(int i=0;i
if(str.charAt(i)=='('){
indexStart = i;
break;
}
}
String strNew = "";
//分割字符串
for(int i=indexStart+1;i
strNew += str.charAt(i);
}
return new JSONObject(strNew);
}
return jo;
}
}
4.测试类 @RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private JokeMapper jokeMapper;
@Test
public void contextLoads() {
String address="这里写你要请求的接口路径如www.baidu.com/getdata";
try {
JSONObject dayLine = new GetJson().getHttpJson(address,1);
System.out.println(dayLine);
//取得data的json数据
JSONArray json= null;
json = dayLine.getJSONArray("joke");
List list= JSON.parseArray(json.toString(),Joke.class);
System.out.println(list);
for (Joke btcoinEntity:list){ //遍历list
jokeMapper.insert(btcoinEntity); //插入数据库
}
} catch (Exception e) {
e.printStackTrace();
}
}
5.这里还有dao层接口JokeMapper、以及JokeMapper.xml、Joke(实体类,需要跟你爬取数据字一一对应)没写,可以通过逆向工程生成,最后运行test 数据成功插入数据库。 ------------参考文章https://blog.csdn.net/rui15111/article/details/80974172
更多推荐




所有评论(0)