创建服务接口代码实现

main
topsun 2 years ago
parent 23f00f568b
commit 1b66257c05

@ -16,4 +16,8 @@ public class AmapConfigConstant {
public static final String DIRECTION_URL = "https://restapi.amap.com/v3/direction/driving"; public static final String DIRECTION_URL = "https://restapi.amap.com/v3/direction/driving";
public static final String DISTRICT_URL = "https://restapi.amap.com/v3/config/district"; public static final String DISTRICT_URL = "https://restapi.amap.com/v3/config/district";
/**
*
*/
public static final String SERVICE_MAP_ADD_URL = "https://tsapi.amap.com/v1/track/service/add";
} }

@ -0,0 +1,8 @@
package com.internal.dto;
import lombok.Data;
@Data
public class ServiceMapResponse {
private String sid;
}

@ -0,0 +1,27 @@
package com.taxi.servicemap.controller;
import com.internal.dto.ResponseResult;
import com.taxi.servicemap.service.ServiceMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
*/
@RestController
public class ServiceController {
@Autowired
public ServiceMapService serviceMapService;
/**
* Service Key
* 线"_"线"-","_"128
* @param name
* @return
*/
@PostMapping("/addService")
public ResponseResult addService(String name){
return serviceMapService.addService(name);
}
}

@ -0,0 +1,50 @@
package com.taxi.servicemap.remote;
import com.internal.contant.AmapConfigConstant;
import com.internal.dto.ResponseResult;
import com.internal.dto.ServiceMapResponse;
import com.taxi.servicemap.service.ServiceMapService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
*
*/
@Service
public class ServiceClient {
@Value("${amap.key}")
private String amapKey;
@Autowired
private RestTemplate restTemplate;
public ResponseResult addService(String name){
//拼接请求的URL
StringBuilder url = new StringBuilder();
url.append(AmapConfigConstant.SERVICE_MAP_ADD_URL);
url.append("?");
url.append("key="+amapKey);
url.append("&");
url.append("name="+name);
ResponseEntity<String> forEntity =
restTemplate.postForEntity(url.toString(),null,String.class);
String bodyStr = forEntity.getBody();
JSONObject result = JSONObject.fromObject(bodyStr);
ServiceMapResponse serviceMapResponse = new ServiceMapResponse();
if(result.has("data")){
JSONObject data = result.getJSONObject("data");
if(data != null && data.has("sid")){
String sid = data.getString("sid");
serviceMapResponse.setSid(sid);
}
}
return ResponseResult.success(serviceMapResponse);
}
}

@ -0,0 +1,19 @@
package com.taxi.servicemap.service;
import com.internal.dto.ResponseResult;
import com.taxi.servicemap.remote.ServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ServiceMapService {
@Autowired
ServiceClient serviceClient;
/**
*
* @return
*/
public ResponseResult addService(String name){
return serviceClient.addService(name);
}
}

@ -0,0 +1,21 @@
{
"data": {
"name": "飞滴出行service",
"sid": 936178
},
"errcode": 10000,
"errdetail": null,
"errmsg": "OK"
}
//终端管理
{
"data": {
"name": "车辆1",
"tid": 685615847,
"sid": 936178
},
"errcode": 10000,
"errdetail": null,
"errmsg": "OK"
}
Loading…
Cancel
Save