java restful接口
用json-lib的jar包输出json串:
public void responseJason(HttpServletResponse response, Object obj){
ObjectMapper objectMapper = new ObjectMapper();
ByteArrayOutputStream baos = new ByteArrayOutputStream();// 向OutPutStream中写入
try {
objectMapper.writeValue(baos, obj);// 对象写入json
} catch (IOException e) {
e.printStackTrace();
}
response.setContentType("application/json; charset=utf-8");
try {
response.getWriter().write(baos.toString());
response.getWriter().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
相对应的,来解析json串:
public String getRegionByIP(String ip){
String url = "http://ip.taobao.com/service/getIpInfo.php?ip="+ip;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(url);
String returnRegion = "";
try {
HttpResponse response = httpClient.execute(httpost);
HttpEntity httpEntity = response.getEntity();
String responseString = EntityUtils.toString(httpEntity);
ObjectMapper objectMapper = new ObjectMapper();
Map map = objectMapper.readValue(responseString, Map.class);
Map dataMap = (Map) map.get("data");
String region = dataMap.get("region").toString();
String city = dataMap.get("city").toString();
returnRegion = region+","+city;
} catch (ClientProtocolException e) {
logger.info("用IP: "+ip+" 取省市出错!");
} catch (IOException e) {
// ipFlag = false;
logger.info("用IP: "+ip+" 取省市出错!");
} catch (Exception e) {
// ipFlag = false;
logger.info("用IP: "+ip+" 取省市出错!");
}
return returnRegion;
}
java restful接口的更多相关文章
- 三种方法实现java调用Restful接口
1,基本介绍 Restful接口的调用,前端一般使用ajax调用,后端可以使用的方法比较多, 本次介绍三种: 1.HttpURLConnection实现 2.HttpClient实现 3.Spring ...
- Java调用RestFul接口
使用Java调用RestFul接口,以POST请求为例,以下提供几种方法: 一.通过HttpURLConnection调用 1 public String postRequest(String url ...
- Java方法通过RestTemplate调用restful接口
背景:项目A需要在代码内部调用项目B的一个restful接口,该接口是POST方式,header中 Authorization为自定义内容,主要传输的内容封装在body中,所以使用到了RestTemp ...
- java调用restful接口的方法
Restful接口的调用,前端一般使用ajax调用,后端可以使用的方法如下: 1.HttpURLConnection实现 2.HttpClient实现 3.Spring的RestTemplate
- Swagger+Spring mvc生成Restful接口文档
简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...
- Java Restful Web Service 学习指南
Restful是一种架构style,目前常说的有restful web service, resultful http.现在热搜榜的微服务,大多数会采用Restful方式. JAX-RS 作为一个Re ...
- Swagger: 一个restful接口文档在线生成+功能测试软件
一.什么是 Swagger? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 ...
- 三种方法实现调用Restful接口
1.基本介绍 Restful接口的调用,前端一般使用ajax调用,后端可以使用的方法比较多, 本次介绍三种: 1.HttpURLConnection实现 2.HttpClient实现 3.Spring ...
- Spring Boot 2.x (十):构建优雅的RESTful接口
RESTful 相信在座的各位对于RESTful都是略有耳闻,那么RESTful到底是什么呢? REST(Representational State Transfer)表述性状态转移是一组架构约束条 ...
随机推荐
- 用php实现交互式工具——How do I write a command-line interactive PHP script?
I want to write a PHP script that I can use from the command line. I want it to prompt and accept in ...
- CentOS7.1 Liberty云平台之环境准备(2)
一.各节点配置Openstack源库 yum install centos-release-openstack-liberty -y 升级YUM源库 yum upgrade 安装Openstackcl ...
- wdcp下nginx+apache混合模式的主机配置
/www/wdlinux/httpd-2.2.22/conf/vhost/xxx.xxx.com.conf <VirtualHost *:88>DocumentRoot /www/web/ ...
- javascript奇技淫巧之位运算符
奇技淫巧:指过于奇巧而无益还让人着迷的技艺与制品. And(与) & Or(或) | Exclusive Or(异或) 或者称 Xor ^ Not(非) ~ 位运算符,我们在日常js开发中其实 ...
- android中XRecyclerView控件的使用
控件的地址:https://github.com/XRecyclerView/XRecyclerView XRecyclerView控件是一个加强版的RecyclerView,可以很方便的实现下拉刷新 ...
- 分布式系统介绍-PNUTS
PNUTS是Yahoo!的分布式数据库系统,支持地域上分布的大规模并发操作.它根据主键的范围区间或者其哈希值的范围区间将表拆分为表单元(Tablet),多个表单元存储在一个服务器上.一个表单元控制器根 ...
- vCenter orchestrator使用范例
- 忘记MySQL root密码重置MySQL root密码
(1)停止mysql# /etc/init.d/mysql stop(2)以不检查权限的方式启动# mysqld --skip-grant-tables &(3)登录mysql修改root用户 ...
- (素材源代码) 猫猫学iOS 之UIDynamic重力、弹性碰撞吸附等现象牛逼Demo
猫猫分享,必须精品 原创文章,欢迎转载. 转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 二:代码 #import "ViewCon ...
- VB 中 copymemory的有关问题
dim a() as long dim b() as bytecopymemory b(0),byval "1234",4 copymemory byval varptr(a(0) ...