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请求):

package com.makeronly.common.tool;

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.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * 作用:使用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;
    }


}

使用:

@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;
}

老接口的访问方式:

老接口内容:

猜你喜欢

转载自my.oschina.net/u/3295928/blog/1798903