正在使用quarkus开发项目。使用jdk17,过去的HttpClient不能用了,经过一番周折,重新实现了HttpClient简单版,代码如下
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Form;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;@ApplicationScoped
public class HttpClient {private Client client;public HttpClient() {this.client = ClientBuilder.newClient();}public Response get(String uri, Map<String, String> queryParams) {WebTarget target = client.target(uri);for (Map.Entry<String, String> entry : queryParams.entrySet()) {target = target.queryParam(entry.getKey(), entry.getValue());}return target.request().get();}public Response post(String uri, Map<String, String> bodyParams) {WebTarget target = client.target(uri);Form form = new Form();for (Map.Entry<String, String> entry : bodyParams.entrySet()) {form.param(entry.getKey(), entry.getValue());}return target.request().post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));}// 其他 HTTP 方法...public void close() {client.close();}
}