演示一下JavaLib框架如何使用?我们从服务端说起
引入
目前JavaLib托管到JitPack。支持gradle
、maven
、sbt
、leiningen
。我在写Java项目时,比较喜欢Maven,写Android时,会用Gradle,其他没用过。下面就以Maven
为例,演示一下如何添加依赖。
1、需要添加仓库
jitpack.io
https://jitpack.io
2、添加依赖
com.github.fengwenyi
JavaLib
0.0.4
如此,pom.xml文件结构大致是这样:
...
com.github.fengwenyi
JavaLib
0.0.4
...
jitpack.io
https://jitpack.io
...
...
注:... 表示,这里可能还有其他更多内容。
Return
现在数据交换大多依赖接口的形式,而数据大多会Json格式。在JavaLib中默认引入Gson(以后可能会默认用Fastjson)。
新建一个枚举类并实现IReturnCode
:
public enum ReturnCode implements IReturnCode {
;
private int code;
private String msg;
ReturnCode() {
}
ReturnCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public int getCode() {
return code;
}
@Override
public String getMsg() {
return msg;
}
}
以登录为例写一个Contoller
:
@RestController
@RequestMapping("/user")
public class UserController {
// login
@PostMapping("/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password) {
Result result = new Result();
if (StringUtil.isNullStr(username)
|| StringUtil.isNullStr(password)) {
result.setResult(ReturnCode.ERROR_DATA_NULL);
} else {
if ("user".equals(username)) {
if ("pwd".equals(password)) {
// 验证成功
/*
// 无数据返回
result.setResult(ReturnCode.SUCCESS);
*/
// 要返回一些数据
Map map = new HashMap<>();
map.put("loginTime", System.currentTimeMillis());
result.setData(ReturnCode.SUCCESS, map);
} else {
result.setResult(ReturnCode.ERROR_USER_PASSWORD_INCORRECT);
}
} else {
result.setResult(ReturnCode.ERROR_USER_USERNAME_INCORRECT);
}
}
return new Gson().toJson(result);
}
}
一些返回码:
// error
ERROR_DATA_NULL(1001, "数据不能为空"),
//----
ERROR_USER_USERNAME_INCORRECT(1101, "用户名不正确"),
ERROR_USER_PASSWORD_INCORRECT(1102, "密码不正确"),
// success
SUCCESS(0, "Sucess")
;
注:这里的代码只是测试,实际开发的验证方式可能更复杂。
测试一下:
{
"code":1101,
"msg":"用户名不正确"
}
{
"code":0,
"msg":"Sucess",
"data":{
"loginTime":1516166553865
}
}
当code=0
时,才会有数据返回。
Get/Post/Put/Delete
先来看web端的代码:
// get
@GetMapping("/get")
public String get(@RequestParam("data") String data) {
return data;
}
// post
@PostMapping("/post")
public String post(@RequestParam("data") String data) {
return data;
}
@PostMapping("/post-json")
public String postJson(HttpServletRequest request) throws IOException {
return RequestUtil.getPostData(request);
}
// put
@PutMapping("/put/{id}")
public String put(@PathVariable("id") int id) {
return String.valueOf(id);
}
// delete
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable("id") int id) {
return String.valueOf(id);
}
在看些Get/Post/Put/Delete提交测试代码:
// Get方式提交
private static void get() throws IOException {
// 提交方式一
String url = host + "/get";
Map map = new HashMap<>();
map.put("data", "data-get-1");
String rs = NetDataUtil.get(url, null, map);
System.out.println("Get=>" + rs);
/*
// 提交方式二
String url = host + "/get?data=data-get-2";
String rs = NetDataUtil.get(url, null, null);
System.out.println(rs);
*/
}
// Post方式提交(表单或json格式数据)
private static void post() throws IOException {
String url = host + "/post";
Map map = new HashMap<>();
map.put("data", "data-表单格式数据");
String rs = NetDataUtil.post(url, null, map);
System.out.println("Post=>" + rs);
// json
String urlJson = host + "/post-json";
Map mapJson = new HashMap<>();
mapJson.put("data", "data-Json-格式数据");
Map header = new HashMap<>();
header.put("Content-Type", "application/json;charset=utf-8");
header.put("Accept", "application/json;charset=utf-8");
String rsJson = NetDataUtil.post(urlJson, header, mapJson);
System.out.println("Post json =>" + rsJson);
}
// Put方式提交
private static void put() throws IOException {
String url = host + "/put/1";
String rs = NetDataUtil.put(url, null, null);
System.out.println("Put=>" + rs);
}
// Delete方式提交
private static void delete() throws IOException {
String url = host + "/delete/2";
Map header = new HashMap<>();
header.put("Content-Type", "application/json;charset=utf-8");
header.put("Accept", "application/json;charset=utf-8");
String rs = NetDataUtil.delete(url, header, null);
System.out.println("Delete=>" + rs);
}
测试代码:
private static String host = "http://localhost:8080/user";
public static void main(String[] args) {
try {
get();
post();
put();
delete();
} catch (IOException e) {
e.printStackTrace();
}
}
测试结果: