尝试使用Jersey2在Java中创建一个restful clint。 这段代码有2个问题: 1. Prety慢需要超过2秒 2.数据绑定有问题,只有2/6数据绑定良好
主Java:
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class JerseyTestClient {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("https://wtfismyip.com/json");
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();
WtfIsMyConfig wtfIsMyConfig = response.readEntity(WtfIsMyConfig.class);
System.out.println(response);
System.out.println(wtfIsMyConfig);
}
}
模型
public class WtfIsMyConfig implements Serializable {
private String YourIPAddress;
private String YourLocation;
private String YourHostname;
private String YourISP;
private String YourTorExit;
private String YourCountryCode;
public WtfIsMyConfig() {
}
//getters setters
}
的pom.xml
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>2.26</version>
</dependency>
</dependencies>
输出控制台:
InboundJaxrsResponse{context=ClientResponse{method=GET, uri=https://wtfismyip.com/json, status=200, reason=OK}}
WtfIsMyConfig{YourIPAddress='192.168.0.0', YourLocation='null', YourHostname='192-168-0-0.pool.digikabel.hu', YourISP='null', YourTorExit='null', YourCountryCode='null'}
Process finished with exit code 0
Compilation completed successfully with 3 warnings in 2s 500ms
请求输出:
{
"YourIPAddress": "192.168.0.0",
"YourLocation": "XXX, 18, Hungary",
"YourHostname": "192-168-0-0.pool.digikabel.hu",
"YourISP": "DIGI Tavkozlesi es Szolgaltato Kft.",
"YourTorExit": "false",
"YourCountryCode": "HU"
}