spring boot + jersey (非spring mvc)使用jersey client發送http post請求,並獲取響應

依賴:

<!-- 使用jersey RESTful 框架 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

實現JerseyClientUtil工具類(實現post和get請求):

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.JerseyClient;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;

/**
 * 作用:使用jerseyclient發送http請求。
 * 主要用於調用老平臺接口(非微服務架構的)
 */
public class JerseyClientUtil {

    private Client client;

    JerseyClientUtil(){}

    /**
     * 靜態內部類實現單例模式
     */
    private static class Singleton{
        private static final JerseyClientUtil jerseyClientUtil = new JerseyClientUtil();
    }

    /**
     * 獲取單例JerseyClientUtil
     * @return
     */
    public static JerseyClientUtil getJerseyClientUtil(){
        return Singleton.jerseyClientUtil;
    }


    /**
     * 初始化默認的clint對象
     */
    private void setDefaultClient(){
        this.client =  ClientBuilder.newClient();
    }


    /**
     * 根據傳入的ClientConfig對象初始化client對象
     * @param config
     */
    private void setConfigClient(ClientConfig config){
        this.client =  ClientBuilder.newClient(config);
    }

    /**
     * 根據ClientConfig初始化Client
     * @param config
     */
    private void initClient(ClientConfig config){
        if(config != null){
            this.setConfigClient(config);
        }else {
            this.setDefaultClient();
        }
    }

    /**
     * 發送http post請求,返回服務器響應信息
     * @param url 請求的URL
     * @param form 發送的form對象
     * @param config jerseyclient實例的配置信息,可以爲空
     * @param tClass 根據此類型處理返回實體
     * @return response Response對象
     */
    public <T> T postInvoke(String url, Form form, ClientConfig config,Class<T> tClass){

        //初始化Client
        this.initClient(config);

        //發送POST的請求
        //應該對響應實體類進行處理,這樣鏈接才能關閉和回收
        //官方:(https://jersey.github.io/documentation/latest/client.html#d0e5255)The underlying connections are opened for each request and closed after the response is received and entity is processed (entity is read).
        Response response = client.target(url).request(MediaType.APPLICATION_FORM_URLENCODED)
                .post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE));
        T resultObject = response.readEntity(tClass);
        return resultObject;
    }

    /**
     * 發送http get請求,返回服務器響應信息
     * @param url 請求的URL
     * @param param 參數map
     * @param config jerseyclient實例的配置信息,可以爲空
     * @param tClass 根據此類型處理返回實體
     * @return response Response對象
     */
    public <T> T getInvoke(String url, Map<String,Object> param, ClientConfig config, Class<T> tClass){

        //初始化Client
        this.initClient(config);
        WebTarget webTarget = client.target(url);
        //添加參數
        for(String key : param.keySet()){
            webTarget = webTarget.queryParam(key,param.get(key));
        }

        T resultObject = webTarget.request().get(tClass);

        return resultObject;

    }


}

 

使用:

@GET
@Path("/sendMessage")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public ResultBean sendMessage(@Content HttpServletRequest request,@QueryParam("phone") String phone){
    //組合參數
    Form from = new Form();
    from.param("phone",phone);
    String response = JerseyClientUtil.getJerseyClientUtil()
            .postInvoke("**************************",
                    from,null,String.class);
    ResultBean resultBean  = new ResultBean(response);
    return resultBean;
}

 

 

老接口的訪問方式:

老接口內容:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章