1.okhttp
Post to a Server–模拟一个post请求
public static final MediaType JSON
= MediaType.get("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(json, JSON);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
模拟get方法
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
两者区别在于post请求需要用到requestbody,赋值给request对象,而get请求则不需要。
2.idea快捷键
ctrl+alt+v 自动补全属性名称
ctrl+alt+n 内联,快速放置原文
ctrl+shift+上下键,整行代码上下移动
3.spring的value注解
在properties里面写好内容,在java类中用@Value注解可以将配置文件中的值注入Java类。
例如:在properties配置文件中有如下一条
github.redirect.uri=http://localhost:8887/callback
则在Java类中可以通过如下注解来注入值
@Value("${github.redirect.uri}")
private String redirectUri;