如何刷网站流量简述网络营销的特点
目录
JSONObject的使用
TreeUtil的使用
EagleMap使用
安装
application.yml配置
springboot导入依赖
配置信息
简单使用
如果想获取这个json字符串里面的distance的值
BeanUtil拷贝注意
JSONObject的使用
假如我现在要处理这样的json数据
可以直接使用JSONUtil.parseObj()方法将其解析成JSONObject对象
JSONObject jsonObject = JSONUtil.parseObj(msg);
现在要获取里面的值,因为type是字符串类型,所以使用getStr(key)方法
String type = jsonObject.getStr("type");
String operation = jsonObject.getStr("operation");
获取数组类型的值,使用getJSONArray()方法,然后获取下标为0的值,并且强转为JSONObject类型。
JSONObject content = (JSONObject) jsonObject.getJSONArray("content").getObj(0);
获取Long类型的值,使用getLong()方法,会自动把字符串类型转换成Long类型
Long parentId = content.getLong("parentId");
获取boolean类型的值,使用getBool()方法
content.getBool("status")
获取int类型的值,使用getInt方法
content.getInt("managerId")
TreeUtil的使用
在处理业务时,我们会碰到要把一个集合数据将其转换成树状结构返回给前端,hutool工具类提供了一个TreeUtil工具类可以帮我们很快的实现这个业务
参考文档:
树结构工具-TreeUtil
示例:
在一个类里要有parentId成员变量,代指父亲id,一般第一层的父id都规定为0
使用
@GetMapping("tree")public String findAllTree() throws JsonProcessingException {// TODO day05 查询机构树// 查询全部机构数据 如果为空,返回空字符串List<OrganDTO> organDTOS = this.organService.findAll(null);// 如果不为空,构造树结构 tips: 使用TreeUtil构建if(CollUtil.isNotEmpty(organDTOS)){//让0作为第一层的父idList<Tree<Long>> trees = TreeUtil.build(organDTOS, 0L, (organDTO, tree) -> {//这三个值一定要设置tree.setId(organDTO.getId());tree.setParentId(organDTO.getParentId());tree.setName(organDTO.getName());//批量把成员变量导入tree.putAll(BeanUtil.beanToMap(organDTO));});// 通过jackson将数据节点转为json字符串 objectMapper.writeValueAsStringreturn objectMapper.writeValueAsString(trees);//如果使用JSONUtil序列化,就会缺少值为空的成员变量}return "";}
刚开始去数据库查询所有数据时,没有分层关系
(organDTO, tree) -> {//这三个值一定要设置tree.setId(organDTO.getId());tree.setParentId(organDTO.getParentId());tree.setName(organDTO.getName());//批量把成员变量导入tree.putAll(BeanUtil.beanToMap(organDTO));}
lambda表达式,organDto代指集合的每一个数据,tree是树型数据(泛型是id类型,为Long),id,parentId,name一定要设置,然后通过putAll方法把organDto的成员变量都赋值给树形数据
// 通过jackson将数据节点转为json字符串 objectMapper.writeValueAsStringreturn objectMapper.writeValueAsString(trees);//如果使用JSONUtil序列化,就会缺少值为空的成员变量
这里使用jackson的ObjectMapper来json序列化,因为JOSNUtil的json序列化不会序列化值为空的成员变量
结果:
tree树形数据会自动添加children成员变量
如果不使用这个工具类,就需要在OrganDto类下添加成员变量
List<OrganDTO>children;
然后使用递归:
private List<OrganDTO>getTree(List<OrganDTO>organDTOS,Long pid){List<OrganDTO>tree=new ArrayList<>();for (OrganDTO organDTO : organDTOS) {OrganDTO node;if(ObjectUtil.equals(organDTO.getParentId(),pid)){node = new OrganDTO();BeanUtil.copyProperties(organDTO,node);node.setChildren(getTree(organDTOs,organDTO.getParentId()))}tree.add(node);}return tree;}
EagleMap使用
文档:
EagleMap - 地图服务中台 黑马程序员·研究院
安装
#拉取镜像 docker pull registry.cn-hangzhou.aliyuncs.com/itheima/eagle-map-server:latestdocker create --name eagle-map-server \ -p 8484:8484 \ -v itcast/eaglemap/app/application.yml:/app/eagle-map-server/conf/application.yml \ -v itcast/eaglemap/app/logs:/app/eagle-map-server/logs \ registry.cn-hangzhou.aliyuncs.com/itheima/eagle-map-server:latest#以上命令指定了端口号、配置文件、日志等信息docker start eagle-map-server
application.yml配置
server:port: 8484 #RESTful服务端口
spring:
# datasource: #数据库的配置
# driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://172.17.2.135:3306/eaglemap?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
# username: root
# password: root123
eagle:#服务模式,可选参数:BASE、COMPLETE,默认:BASE#BASE模式中无需配置数据库,只保留基础的地图服务service-mode: BASE#默认策略,可选参数:BAIDU、AMAP、NONE,默认:NONE#如果指定了默认策略,在接口中不指定服务商时才用的策略,如果指定NONE,有系统来决定default-provider-strategy: NONEbaidu-web-api: https://api.map.baidu.com #百度地图web api服务地址baidu-ying-yan-api: http://yingyan.baidu.com #百度地图鹰眼服务地址amap-web-api: https://restapi.amap.com #高德地图web api服务地址amap-ts-api: https://tsapi.amap.com #高德地图猎鹰服务地址admin: #可视化管理系统的配置user: eagle #用户名password: b09315ea09c6d3b5680094257f1f70e4 #密码:eagle,md5加密后#RSA的私钥#RSA的公钥token-time: 24 #token有效期,单位:小时timeout: 30000 #请求超时时间,单位:毫秒trace-image: #轨迹缩略图scale: 0.9 #缩放比,默认0.9background: "#FFFFFF" #背景颜色trace-color: "#6699FF" #轨迹颜色stroke-width: 3 #画笔宽度width: 300 #生成图片的宽度height: 300 #生成图片的高度
# baidu: #百度相关的配置
# enable: false #是否启用
# name: 百度地图 #服务商的名称
# server: #服务端账号的配置
# id: xxxxx
# name: xxxxx
# ak: xxxxx
# type: xxxxx
# browser: #浏览器端账号的配置
# id: xxxxx
# name: xxxxx
# ak: xxxxx
# type: xxxxx
# trace-servers: #百度地图中的轨迹服务
# - id: xxxxx
# name: xxxxx
# type: xxxxx
# date: xxxxx
# - id: xxxxx
# name: xxxxx
# type: xxxxx
# date: xxxxxamap: #高德相关的配置enable: true #是否启用name: 高德地图 #服务商的名称server: #服务端账号的配置name: EagleMap-Serverak: 85db5737cb07bd69fd79c47db37edtype: Web服务browser: #浏览器端账号的配置name: EagleMap-JSak: 0249fa505ac60236af082df54413266sk: 52decdd5151bac3021fa6736979258type: Web端
amap: #高德相关的配置
enable: true #是否启用
name: 高德地图 #服务商的名称
server: #服务端账号的配置
name: EagleMap-Server
ak: 85db5737cb07bd69fd79c47db37ed
type: Web服务
browser: #浏览器端账号的配置
name: EagleMap-JS
ak: 0249fa505ac60236af082df54413266
sk: 52decdd5151bac3021fa6736979258
type: Web端这里配置了高德地图,ak值需要我们自己去申请
我的应用 | 高德控制台
然后创建新的应用,创建完成,添加key
这里分别创建两个key,一个是服务端,一个是web端
创建完成
springboot导入依赖
<dependency><groupId>com.itheima.em</groupId><artifactId>eaglemap-spring-boot-starter</artifactId><version>1.0</version></dependency>
配置信息
eagle:host: 192.168.150.101 #EagleMap服务地址port: 8484 #EagleMap服务端口timeout: 20000 #http请求的超时时间
简单使用
@SpringBootTest
public class Test2 {@ResourceEagleMapTemplate eagleMapTemplate;@Testpublic void test01(){//详细地址转换坐标String address = "北京大学";GeoResult geoResult = this.eagleMapTemplate.opsForBase().geoCode(ProviderEnum.AMAP, address, null);System.out.println(geoResult);}
@Testpublic void driving() {//驾车路线规划Coordinate origin = new Coordinate(116.34411597643727, 40.06061915065967);Coordinate destination = new Coordinate(116.398704, 39.907539);String result = this.eagleMapTemplate.opsForDirection().driving(ProviderEnum.AMAP, origin, destination);System.out.println(result);}
一共有三条路线
如果想获取这个json字符串里面的distance的值
一般方法:
JSONObejct jsonObject =JSONUtil.parseObject(msg);
JSONObject route=jsonObject.getObj("route");
JSONObject path==route.getJSONArray("path").getObj(0);
Long distance=path.getLong("distance");
特殊方法
JSONObejct jsonObject =JSONUtil.parseObject(msg);
Long distance=Convert.toLong(jsonObject.getByPath("route.paths[0].distance"));//只要我们规定要要取到值的路径,hutool工具类会直接帮我们获取到值
BeanUtil拷贝注意
在将transportLine的成员变量的值拷贝给transportLineDate数据时,不会把transportLine值为空的成员变量进行拷贝,也不会把值覆盖到transportLineDate原来的type,startOrganId,startOrganName,endOrganId,endOrganName
//拷贝数据,忽略null值以及不能修改的字段BeanUtil.copyProperties(transportLine, transportLineData, CopyOptions.create().setIgnoreNullValue(true).setIgnoreProperties("type", "startOrganId", "startOrganName", "endOrganId", "endOrganName"));