用javax.ws.rs.client.Invocation queryParam 执行url中带参数的请求
来自于百度AI,为了实际需要,改成我自己的环境。
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.jackson.JacksonFeature; import org.glassfish.jersey.media.multipart.MultiPartFeature; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import java.util.*; public static void main(String[] args) { ClientConfig configuration = new ClientConfig(); configuration = configuration.property(ClientProperties.CONNECT_TIMEOUT, 30000); configuration = configuration.property(ClientProperties.READ_TIMEOUT, 30000); Client client = ClientBuilder.newBuilder().withConfig(configuration).hostnameVerifier((s1, s2) -> true).register(new JacksonFeature()).build(); // 定义目标服务的WebTarget,通用:Client client = ClientBuilder.newClient(); WebTarget target = client.target("https://urlpath/list?limit=1000000&service=22503"); // 添加查询参数 String paramName = "service"; Integer paramValue = 22503; WebTarget withQueryParam = target.queryParam(paramName, paramValue); String token = "actual token value"; // 发送请求,例如Post请求,通用:String response = withQueryParam.request().header("token",token).post(null,String.class); String response = withQueryParam.register(MultiPartFeature.class) .request(MediaType.APPLICATION_JSON).header("token", token).post(null, String.class); // 输出响应 System.out.println(response); // 关闭客户端 client.close(); }
之前把baseurl和相对路径分开用 client.target(baseUrl).path(path),查询结果为空,后来改成 client.target(absoluteUrl); 能正确返回结果。所以path(relativePath)用的时候有限制,但是还不知道原因,暂时不管了,用 client.target(absoluteUrl) 就行
public Invocation.Builder getBuilder(String relativePath, Map<String, Object> queryParameters) { ClientConfig configuration = new ClientConfig(); configuration = configuration.property(ClientProperties.CONNECT_TIMEOUT, 30000); configuration = configuration.property(ClientProperties.READ_TIMEOUT, 30000); Client client = ClientBuilder.newBuilder().withConfig(configuration).hostnameVerifier((s1, s2) -> true).register(new JacksonFeature()).build(); if (baseUrl == null) { baseUrl = EnvConfig.base_url; } String absoluteUrl = baseUrl + relativePath; WebTarget webTarget = client.target(absoluteUrl); for (Map.Entry entry : queryParameters.entrySet()) { webTarget = webTarget.queryParam(entry.getKey().toString(), entry.getKey().toString()); } return webTarget.register(MultiPartFeature.class) .request(MediaType.APPLICATION_JSON) .header("token", token); } public <T> T doQueryParamPostRequest(String url, Map<String, Object> queryParams, Object payload, Class<T> klass) { Invocation.Builder invoker = this.getBuilder(url, queryParams); try { return invoker.post(Entity.entity(payload, MediaType.APPLICATION_JSON), klass); } catch (ProcessingException ex) { try { return invoker.post(Entity.entity(payload, MediaType.APPLICATION_JSON), klass); } catch (ProcessingException ex1) { CommonUtil.sleep(3000); return invoker.post(Entity.entity(payload, MediaType.APPLICATION_JSON), klass); } } }