近期作接口對接時,對方直接返回整個html頁面,把我整懵了,特此記錄下。css
網上有不少人提出採用 中轉頁面 的方式,本文另闢蹊徑,採起後端方式解決。html
1. 導包java
<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.12.1</version> </dependency>
2. 上代碼node
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"); 的話,中文會變成???web
3. 配置文件spring
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>
這裏沒打算說的,但當時本身忘記加靜態資源配置了,致使樣式未顯示出來。(樣式、圖片等靜態資源找對方要)spring-mvc
至此,完成。mvc
除暴安良我心願,一身正氣蕩人間app