使用框架 Spring boot
Spring boot 集成
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
JerseyClientUtil 类
集成简单的GET、POST请求(包括http https)
package com.tenmax.util;
import javax.net.ssl.SSLContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.SslConfigurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class JerseyClientUtil {
@Autowired
AppConfiguration appConfiguration;
private String acceptedResponseMediaTypes;
private String entityConverteMediaType;
public JerseyClientUtil setAcceptedResponseMediaTypes(String mediaTypes) {
this.acceptedResponseMediaTypes = mediaTypes;
return this;
}
public JerseyClientUtil setEntityConverteMediaType(String mediaTypes) {
this.entityConverteMediaType = mediaTypes;
return this;
}
private SslConfigurator buildSSLConfigOneWay() {
return SslConfigurator.newInstance()
.trustStoreFile(appConfiguration.getTrustStoreFile())
.trustStorePassword(appConfiguration.getTrustStorePassword());
}
private SslConfigurator buildSSLConfigBidirectional() {
return SslConfigurator.newInstance()
.trustStoreFile(appConfiguration.getTrustStoreFile())
.trustStorePassword(appConfiguration.getTrustStorePassword())
.keyStoreFile(appConfiguration.getKeyStoreFile())
.keyStorePassword(appConfiguration.getKeyPassword());
}
private Client buildSSLClient(SslConfigurator sslConfig) {
SSLContext sslContext = sslConfig.createSSLContext();
return ClientBuilder.newBuilder()
.sslContext(sslContext)
.build();
}
public Response getHttpsOne(String url){
checkinitSuccessGet();
SslConfigurator sslConfig = buildSSLConfigOneWay();
Client client = buildSSLClient(sslConfig);
return client.target(url)
.request(acceptedResponseMediaTypes)
.get();
}
public Response getHttpsTwo(String url){
checkinitSuccessGet();
SslConfigurator sslConfig = buildSSLConfigBidirectional();
Client client = buildSSLClient(sslConfig);
return client.target(url)
.request(acceptedResponseMediaTypes)
.get();
}
public Response postHttpsOne(String url, Object obj){
checkinitSuccessPost();
SslConfigurator sslConfig = buildSSLConfigOneWay();
Client client = buildSSLClient(sslConfig);
return client.target(url)
.request(acceptedResponseMediaTypes)
.post(Entity.entity(obj, entityConverteMediaType));
}
public Response postHttpsTwo(String url, Object obj){
checkinitSuccessPost();
SslConfigurator sslConfig = buildSSLConfigBidirectional();
Client client = buildSSLClient(sslConfig);
return client.target(url)
.request(acceptedResponseMediaTypes)
.post(Entity.entity(obj, entityConverteMediaType));
}
public Response getHttp(String url){
checkinitSuccessGet();
return ClientBuilder.newClient()
.target(appConfiguration.getWebServer())
.request(acceptedResponseMediaTypes)
.get();
}
public Response postHttp(String url, Object obj){
checkinitSuccessPost();
return ClientBuilder.newClient()
.target(appConfiguration.getWebServer())
.request(acceptedResponseMediaTypes)
.post(Entity.entity(obj, entityConverteMediaType));
}
private void checkinitSuccessPost(){
if (entityConverteMediaType == null||acceptedResponseMediaTypes == null) {
System.out.println("JerseyClientUtil init faild when POST! please init entityConverteMediaType and acceptedResponseMediaTypes!");
return;
}
}
private void checkinitSuccessGet(){
if (acceptedResponseMediaTypes == null) {
System.out.println("JerseyClientUtil init faild when GET! please init acceptedResponseMediaTypes!");
return;
}
}
}
调用示例(POST请求)
@Autowired
JerseyClientUtil jerseyClientUtil;
Response response = jerseyClientUtil.setAcceptedResponseMediaTypes(MediaType.APPLICATION_JSON)
.setEntityConverteMediaType(MediaType.APPLICATION_JSON)
.postHttp(appConfiguration.getWebServer(), new User(userId, password));
YourObject obj = response.readEntity(YourObject .class);
//TODO: 逻辑代码