接口Demo(Java)

结构路径

util 工具类

把需要用到的工具类创建在这里

controller

  • 新建 CommonController 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Controller
@RequestMapping("/api")
public class CommonController {

@Autowired
public CommonService commonService;

@ResponseBody
@RequestMapping( value = "/GRXXCJ.do",method=RequestMethod.POST)
public Object pushToMain() {
return this.commonService.GRXXCJ();
}

@ResponseBody
@RequestMapping( value = "/photo.do",method=RequestMethod.POST)
public Object pushToCloud() {
return this.commonService.photo();
}

@ResponseBody
@RequestMapping( value = "/GRXXZP.do",method=RequestMethod.POST)
public Object pushToMainZP() {
return this.commonService.GRXXZP();
}

}

service

  • 新建 CommonService 类
1
2
3
4
5
public interface CommonService {
Object GRXXCJ();
Object photo();
Object GRXXZP();
}

service.impl

  • 默认核心代码都写在这里
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@Service
public class CommonServiceImpl implements CommonService {

@Config(name = "refluxPath", defaultValue = "", description = "refluxPath")
private static String refluxPath;
@Config(name = "source", defaultValue = "", description = "source")
private static String source;
@Config(name = "sourceZP", defaultValue = "", description = "source")
private static String dest;
@Config(name = "destZP", defaultValue = "", description = "dest")
private static String secretKey;
@Config(name = "secretKeyZP", defaultValue = "", description = "secretKey")
private static String type;
@Config(name = "typeZP", defaultValue = "", description = "type")

//从数据库获数据,并回流给其他接口
public Object GRXXZP() {
String pathUrl = refluxPath;
long beginDate = new Date().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHMMss");
String sd = sdf.format(new Date(beginDate));
String timestamp = sd;
String sign = sourceZP + destZP + secretKeyZP;
String sign256 = SHA256Util.encrypt(sign);
JSONObject json = new JSONObject();
List<Map<String, Object>> data = null;
String sql = "SELECT * FROM T_IT_GRXXCJ_ZP";
try {
data = DbUtil.query(sql, null);
System.out.println("data======================" + data);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// data = HttpUtils.getParameterMap();
String sdata = data.toString();
byte[] bdata = sdata.getBytes();

try {
String base64data = Base64Utils.encode(bdata);
String gzipdata = ZipCompressor.gzip(base64data);
json.put("source", sourceZP);
json.put("dest", destZP);
json.put("sign", sign256);
json.put("timestamp", timestamp);
json.put("type", typeZP);
json.put("data", gzipdata);
JsonElement jedata = com.wisedu.emap.it.util.JsonUtils.toJsonElement(json);
HttpUtils.post(pathUrl, null, jedata);
// HttpRequestUtil.doHttpPost(json.toString(),pathUrl);
// JSONObject json_to_data = json.getJSONObject("data");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return json;

}
}

本文地址:https://tonysteven.github.io/2018/08/07/java-RESTful-api/
转载请注明出处,谢谢!

坚持原创技术分享,您的支持将鼓励我继续创作!