接口返回html页面展示在浏览器

近期做接口对接时,对方直接返回整个html页面,把我整懵了,特此记录下。

网上有很多人提出采用 中转页面 的方式,本文另辟蹊径,采取后端方式解决。

1. 导包

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.12.1</version>
</dependency>

 

2. 上代码

package cn.sd.service.impl;

import cn.sd.service.SpeedServiceI;
import cn.sd.utils.OutKey;
import cn.sd.utils.WxApi;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 清风明月
 * @description
 * @date 2019/12/7 18:59
 */
@Service
public class SpeedServiceImpl implements SpeedServiceI {
    private static final Logger logger = LoggerFactory.getLogger(SpeedServiceImpl.class);
    /**
     * 某公众号的appid
     */
    private String appId = "***********";
    /**
     * 约定的密钥
     */
    private String clearly = "###########";

    @Override
    public void showHtmlPage(String openId, String phone, String card, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            writer = new PrintWriter(System.out);
            logger.error("IO异常:", e);
        }

        Map userInfo = WxApi.getUserInfo(appId, openId);
        logger.info("关注用户信息:" + userInfo);

        Map bindUserInfo = WxApi.getBindingMobileEntity(appId, openId);
        logger.info("绑定用户信息:" + bindUserInfo);

        if (userInfo == null || bindUserInfo == null || userInfo.isEmpty() || bindUserInfo.isEmpty()) {
            logger.info("请关注【某公众号】并绑定手机号后访问");
            writer.println("请关注【某公众号】并绑定手机号后访问");
        } else {
            //1 关注
            String subscribe = String.valueOf(userInfo.get("subscribe"));
            //Y 绑定
            String bindStatus = (String) bindUserInfo.get("bindingStatus");
            logger.info("bindStatus:" + bindStatus + " && subscribe:" + subscribe);

            if ("Y".equals(bindStatus) && "1".equals(subscribe)) {
                //手机号码加密
                String nbr = OutKey.encrypt(phone, clearly);
                //中文加密
                String area = OutKey.encrypt(card, clearly);
                String speedUrl = "http://***.**.***.**:****/***?nbr={nbr}&area={area}&clearly={clearly}";
                Map<String, String> map = new HashMap<>(4);
                map.put("nbr", nbr);
                map.put("area", area);
                map.put("clearly", clearly);
                logger.info("map:{}", map);
                RestTemplate restTemplate = new RestTemplate();
                String html = restTemplate.getForObject(speedUrl, String.class, map);
                Document document = Jsoup.parse(html);
                logger.info("document:{}", document);
                writer.println(document.outerHtml());
            } else {
                logger.info("请绑定手机号后访问");
                writer.println("请绑定手机号后访问");
            }
        }
        writer.close();
    }
}

不加 response.setContentType("text/html;charset=utf-8"); 的话,中文会变成???

 

3. 配置文件

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--  加载静态资源  -->
    <mvc:annotation-driven />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/js/**" location="/js/" />

    <!-- 包扫描 -->
    <context:component-scan base-package="cn.sd.controller, cn.sd.service"/>

</beans>

这里没打算说的,但当时自己忘记加静态资源配置了,导致样式未显示出来。(样式、图片等静态资源找对方要)

 

至此,完成。

除暴安良我心愿,一身正气荡人间

posted @ 2019-12-07 22:00  品味Code  阅读(9739)  评论(0编辑  收藏  举报