boot使用HttpClient发送请求,及解析xml字符串,并转储为json文件

本文介绍了如何在Spring Boot应用中利用HttpClient发送请求,解析XML字符串,并将数据转换为Java对象,最终将对象列表写入JSON文件。文章涵盖了maven依赖、log4j2配置、全局常量设置、HTTP请求方法、XML解析到Bean以及JSON文件的生成过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境基础:maven+idea
技术:SpringBoot+HttpClient
一:导入maven坐标

<!--boot项目父工程,使用boot必须导入-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
   	<!--boot项目标配 -begin-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <!--去掉springboot本身的log配置-->
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
   	<!--boot项目标配 -end-->

    <!--log4j2依赖包 -begin -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
  </dependencies>
    <!--log4j2依赖包 -end -->

    <!-- httpClient所需包 -begin -->
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpcore</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
    </dependency>
    <!-- httpClienthttp所需包 -end -->

    <!-- xml数据解析所需包 -begin  -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.4</version>
    </dependency>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.6</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.4</version>
    </dependency>
    <!-- xml数据解析所需包 -end  -->

  <build>
    <plugins>
      <!-- boot打包的maven插件 -->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

二:log4j2日志配置文件

(1)Boot默认加载配置application.yml:

logging:
  config: classpath:log4j2.xml # 指定log4j配置文件的位置

(2)log4j2配置文件(可直接复制):

<Configuration status="INFO" monitorInterval="30">
    <Properties>
        <!--  配置日志输出路径  -->
        <Property name="logpath">D:\study\blog_layui\log</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
        </Console>
        <RollingFile name="debug" fileName="${logpath}/debug/debug.log"
                     filePattern="${logpath}/debug/debug_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            </Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/debug" maxDepth="1">
                    <IfFileName glob="debug_*.log"/>
                    <IfLastModified age="15d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <RollingFile name="info" fileName="${logpath}/info/info.log"
                     filePattern="${logpath}/info/info_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            </Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/info" maxDepth="1">
                    <IfFileName glob="info_*.log"/>
                    <IfLastModified age="15d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <RollingFile name="warn" fileName="${logpath}/warn/warn.log"
                     filePattern="${logpath}/warn/warn_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            </Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/warn" maxDepth="1">
                    <IfFileName glob="warn_*.log"/>
                    <IfLastModified age="15d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
        <RollingFile name="error" fileName="${logpath}/error/error.log"
                     filePattern="${logpath}/error/error_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                <!--   每个文件最大50M -->
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            </Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/error" maxDepth="1">
                    <IfFileName glob="error_*.log"/>
                    <!-- 设置最大保存时间为15天-->
                    <IfLastModified age="15d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>

    </Appenders>
    <!--切换输出级别-->
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="debug"/>
            <AppenderRef ref="info"/>
            <AppenderRef ref="warn"/>
            <AppenderRef ref="error"/>
        </Root>
    </Loggers>
</Configuration>

三:全局常量

    private static final String path = "C:\\Users\\test.json";
    private static final String getUrl = "get请求的路径";

四:发送请求的方法

	/**
     * 发送接口请求
     * @param url
     * @return
     */
    private String sendUrl(String url){
        //获取httpClient对象,用来发送请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建get请求
        HttpGet get = new HttpGet(url);
        //设置超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(10000).setConnectTimeout(20000)
                .setConnectionRequestTimeout(10000).build();
        get.setConfig(requestConfig);
        //创建返回对象
        String context = StringUtils.EMPTY;
        //创建响应对象
        CloseableHttpResponse response = null;
        try {
            //发送请求,并接收响应内容
            response = httpClient.execute(get);
            //获取响应体
            HttpEntity entity = response.getEntity();
            //拿到返回的内容,这儿是一个xml文件的字符串,所以用的也是xml解析
            context = EntityUtils.toString(entity, HTTP.UTF_8);
        } catch (Exception e) {
            e.getStackTrace();
        } finally {
            try {
                response.close();
                get.abort();
                httpClient.close();
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
        return context;
    }

五:创建实体bean(只作参考,bean根据自己实际情况创建)

public class XmlBean {
    private String last;
    private String id;
    private String tid;
    private String name;
    private String type;
    private String dt;
    private String note;
	//各个字段的get(),set()方法就不写了,太长了
}

六:解析xml字符串转换为对象

xml字符串示例:

<video>
	<last>2020-09-15 01:47:14</last>
	<id>36828</id>
	<tid>16</tid>
	<name><![CDATA[你喜欢勃拉姆斯吗]]></name>
	<type>韩剧</type>
	<dt>yjm3u8</dt>
	<note><![CDATA[第05集]]></note>
</video>

java解析xml数据到bean中:

    /**
     * 转换xml字符串为对应的Bean集合
     * @param content
     * @return
     * @throws Exception
     */
    private List<XmlBean> changeBean(String content) throws Exception{
        List<XmlBean> xmlBeans = new ArrayList<>();

        //1.创建DocumentBuilderFactory对象
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource inputSource = new InputSource();
        inputSource.setCharacterStream(new StringReader(content));
        //获取字符串转换的DOM对象
        Document document = builder.parse(inputSource);
        //获取DOM中所有标签名为“video”的节点列表
        NodeList nodeList = document.getElementsByTagName("video");
        //for循环遍历节点列表
        for (int i = 0; i < nodeList.getLength(); i++){
            //获取单个标签节点对象
            Node node = nodeList.item(i);
            NodeList childNodes = node.getChildNodes();
            //创建要返回的bean实体
            XmlBean bean = new XmlBean();
            //为返回的bean实体赋值
            bean.setLast(String.valueOf(childNodes.item(0).getFirstChild().getNodeValue()));
            bean.setId(String.valueOf(childNodes.item(1).getFirstChild().getNodeValue()));
            bean.setTid(String.valueOf(childNodes.item(2).getFirstChild().getNodeValue()));
            bean.setName(String.valueOf(childNodes.item(3).getFirstChild().getNodeValue()));
            bean.setType(String.valueOf(childNodes.item(4).getFirstChild().getNodeValue()));
            bean.setDt(String.valueOf(childNodes.item(5).getFirstChild().getNodeValue()));
            bean.setNote(String.valueOf(childNodes.item(6).getFirstChild().getNodeValue()));
            xmlBeans.add(bean);
        }
        return xmlBeans;
    }

七:把bean的list集合写到json文件中

/**
     * 转储文件
     * @param xmlBeanList
     * @return
     */
    private boolean writeToFile(List<?> xmlBeanList){
        //将list通过fastJson转为字符串
        String content = JSONObject.toJSONString(xmlBeanList);
        BufferedWriter writer = null;
        //使用path全局变量
        File file = new File(path);
        //如果文件不存在,则新建一个
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //写入
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
            writer.write(content);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

八:最后再来一个主函数代码,作为示例

    @RequestMapping("/send")
    public String GetJson(){
        try {
            HttpResponse response = this.sendUrl(getUrl);
            HttpEntity entity = response.getEntity();
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200){
                logger.error("发送失败");
                return "Send Fail!";
            }
            String content = EntityUtils.toString(entity, HTTP.UTF_8);
            List<XmlBean> xmlBeanList = this.changeBean(content);
            this.writeToFile(xmlBeanList);
            return "Send Success!";
        } catch (Exception e) {
            logger.error(e.getMessage());
            return "Send Fail!";
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值