http://httpbin.org/#/Status_codes
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
/**
* @author KawYang
* Created by MacBook Pro on 2022/03/27.
*/
public class RequestUtil {
private static final String POST = "POST";
private static final String GET = "GET";
public static void appadd(String path, String method , String params) {
try {
//创建连接
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod(method);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
//POST请求
if (POST.equals(method)) {
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
}
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), StandardCharsets.UTF_8);
sb.append(lines);
}
System.out.println(sb);
reader.close();
// 断开连接
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
final HashMap<String, Object> params = new HashMap<>(3);
appadd("url", POST, mapToJSON(params));
appadd(mapToGetUrl("url", params), GET, null);
}
/**
* 填充GET请求地址
* @param url baseUrl
* @param map params
* @return url
*/
private static String mapToGetUrl(String url , Map<String, Object> map){
final StringBuilder builder = new StringBuilder(url);
builder.append("?");
map.forEach((key, value) -> {
builder.append(String.format("%s=%s&", key, value));
});
return builder.toString();
}
/**
* map转json
* @param map Map
* @return json String
*/
private static String mapToJSON(Map<String, Object> map){
final StringBuilder builder = new StringBuilder("{");
map.forEach((key, value) -> {
builder.append(String.format("\"%s\":\"%s\",", key, value));
});
builder.append("}");
return builder.toString().replace(",}", "}");
}
}
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.5.8version>
dependency>
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* @author KawYang
* Created by MacBook Pro on 2022/03/27.
*/
public class HutoolRequestUtil {
public static void main(String[] args) {
final HashMap<String, Object> params = new HashMap<>();
params.put("key", "value");
doGet("url", params);
doPost("url", params);
}
/**
* Get
* @param url url
* @param params params
* @return response
*/
public static String doGet(String url, Map<String, Object> params){
final String response = HttpUtil.get(url, params);
System.out.println(response);
return response;
}
/**
* Post
* @param url url
* @param params params
* @return response
*/
public static String doPost(String url, Map<String, Object> params){
final HttpRequest request = HttpUtil.createPost(url).body(JSONObject.toJSONString(params));
request.header("Content-Type","application/json");
request.setConnectionTimeout(2000);
final HttpResponse execute = request.execute();
System.out.println(execute.body());
return execute.body();
}
}
public static void main(String[] args) {
final Field[] declaredFields = ClassEs.class.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
final ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
try {
System.out.println(String.format("@ApiModelProperty(\"%s\")", annotation.value()));
}catch (Exception e){}
System.out.println(String.format("@TableField(value = \"%s\")", StrUtil.toUnderlineCase( field.getName()).toUpperCase()));
System.out.println(String.format("private %s %s;", field.getType().getName().substring(field.getType().getName().lastIndexOf(".") + 1), field.getName()));
System.out.println();
}
}