最近用到第三方接口,就是一个另一个接口向我们发送httpPost请求,我们接受数据,然后返回给他们成功或失败
话不多说,直接上代码
注意1、我们接受请求,通常都是用数据流,来接受,这里直接用了springMVC的@RequestBody,它可以将请求体中的数据直接转为string字符串,这就直接拿到数据了。单数如果是get请求是没有请求体的,这时直接将该注解去掉即可。
注意2、在我处理完数据返回信息的时候,又出现了问题,我将返回的信息封装在map集合里,然后将map集合转换为jsonString,然后我用postMain测试,返回结果中中文乱码,显示编码方式为ISO-8859-1,原因并不奇怪,应为字符串没有经过任何的处理。但是如果我直接将map集合返回,不转为字符串,也不经过任何的处理,那么postmain收到的数据中中文是不会乱码的。原因就在于如果直接返回map,那么会默认将map解析为json数据,此时转为json的时候默认编码方式为UTF-8.查看postMain果然显示也是UTF-8编码。于是问题的到解决。
//请求方式:POST请求,Content-Type:application/json;charset=utf-8,
@RequestMapping("/insertArticle.do")
@ResponseBody
public Map insertArticle(@RequestBody String json, HttpServletRequest request,HttpServletResponse response)throws Exception{
// 这里如果是get请求,将@RequestBody去掉。因为get请求没有请求体(body)
// RequestBody:将http请求中的body中的数据拿到,并转换为String格式的数据
JSONObject jsonObject = JSON.parseObject(json);
HashMap<String, Object> resultMap = isFalse(jsonObject);
if(ipFilter(request)){
if((Boolean)resultMap.get("success")){ // 文章存在,各个参数值正确
Article article = new Article();
String articleID = jsonObject.getString("articleID");
String columnID = jsonObject.getString("columnID");
Boolean crawlPic = Boolean.valueOf(jsonObject.getString("crawlPic")); // 是否抓取图片
Column column = columnService.getColumnById(Integer.valueOf(columnID).intValue());
article.setArticleSiteId(column.getSiteId());
String content = "";
String imgUrl = "";
// 如果需要抓取图片,就将原文中的图片下载下来,保存到本地服务器,并将原文中的地址替换为本地地址
if(crawlPic){
content = ImageHunter.catchImages(jsonObject.getString("content"), column.getSiteId().toString());
imgUrl = ImageHunter.loadToStorage(jsonObject.getString("imgUrl"), column.getSiteId().toString());
}else{ // 不抓取图片,就将图片中地址域名给替换掉
content = domainNameReplace(jsonObject.getString("content"));
imgUrl = domainNameReplace(jsonObject.getString("imgUrl"));
}
// 设置参数
article.setArticleColumnId(Integer.valueOf(columnID).intValue());
article.setArticleTitle(jsonObject.getString("title"));
article.setArticleType(Short.valueOf(jsonObject.getString("articleType")).shortValue());
article.setSysModifyUser(jsonObject.getString("optUser"));
article.setArticleStatus(Short.valueOf(jsonObject.getString("status")).shortValue());
article.setArticleContent(content);
article.setArticleSubTitle(jsonObject.getString("subTitle"));
article.setArticleLeadTitle(jsonObject.getString("leadTitle"));
article.setArticleAbstract(jsonObject.getString("abstract"));
article.setArticleSource(jsonObject.getString("source"));
article.setArticleTag(jsonObject.getString("tag"));
article.setArticleAuthor(jsonObject.getString("author"));
article.setArticleEditor(jsonObject.getString("editor"));
article.setArticleLiability(jsonObject.getString("liability"));
article.setArticlePicBig(imgUrl);
article.setArticleWordCount(Long.valueOf(SensitiveWordsFilterUtils.trimHtml2Txt(article.getArticleContent()).length()));
jsonObject.getString("author");
jsonObject.getString("editor");
jsonObject.getString("liability");
jsonObject.getString("imgUrl");
if(Long.valueOf(articleID).longValue() == 0 ){
// 新建
articleService.insertSelective(article); // 该方法内有新建articlePub的操作
resultMap.put("articleID",article.getId());
}else{
// 更新:更新g_article_pub_lib表中,所有articleId 为articleID的数据
article.setId(Long.valueOf(articleID).longValue());
articleService.updateSelective(article);
ArticlePubExample example = new ArticlePubExample();
example.createCriteria().andArticleIdEqualTo(Long.valueOf(articleID));
List<ArticlePub> articlePubs = pubService.listByExample(example);
for(ArticlePub artPub :articlePubs){
artPub.setArticleId(Long.valueOf(articleID).longValue());
artPub.setArticleSiteId(Long.valueOf(article.getArticleSiteId()));
artPub.setArticleTitle(article.getArticleTitle());
artPub.setArticleType(article.getArticleType());
artPub.setSysModifyUser(article.getSysModifyUser());
artPub.setArticleStatus(article.getArticleStatus());
artPub.setArticleAbstract(article.getArticleAbstract());
artPub.setArticleSource(article.getArticleSource());
artPub.setArticleEditor(article.getArticleEditor());
artPub.setArticlePicBig(imgUrl);
artPub.setColumnId(Integer.parseInt(columnID));
pubService.updateSelective(artPub);
}
}
}
}else{
HashMap<String ,Object> result = new HashMap<>();
result.put("success",false);
result.put("msg","该ip无权访问此接口,请联系管理员配置接口白名单");
return result;
}
return resultMap;
}