spring boot + jersey (non-spring mvc) use jersey client to send http post request and get response

rely:

<!-- Using jersey RESTful framework -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

Implement the JerseyClientUtil tool class (for the time being, only post requests are implemented):

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;

/**
 * Role: use jerseyclient to send http requests.
 * Mainly used to call the old platform interface (non-microservice architecture)
 */
public class JerseyClientUtil {

    private Client client;

    JerseyClientUtil(){}

    /**
     * Static inner class implements singleton pattern
     */
    private static class Singleton{
        private static final JerseyClientUtil jerseyClientUtil = new JerseyClientUtil();
    }

    /**
     * Get the singleton JerseyClientUtil
     * @return
     */
    public static JerseyClientUtil getJerseyClientUtil(){
        return Singleton.jerseyClientUtil;
    }


    /**
     * Initialize the default clint object
     */
    private void setDefaultClient(){
        this.client =  ClientBuilder.newClient();
    }


    /**
     * Initialize the client object according to the incoming ClientConfig object
     * @param config
     */
    private void setConfigClient(ClientConfig config){
        this.client =  ClientBuilder.newClient(config);
    }

    /**
     * Initialize Client according to ClientConfig
     * @param config
     */
    private void initClient(ClientConfig config){
        if(config != null){
            this.setConfigClient(config);
        }else {
            this.setDefaultClient();
        }
    }

    /**
     * Send http post request and return server response information
     * @param url the requested URL
     * @param form the form object sent
     * @param config jerseyclient instance configuration information, can be empty
     * @param tClass handles the returned entity according to this type
     * @return response Response对象
     */
    public <T> T postInvoke(String url, Form form, ClientConfig config,Class<T> tClass){

        //Initialize Client
        this.initClient(config);

        //Send the POST request
        // The response entity class should be processed so that the link can be closed and recycled
        //官方:(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;
    }


}

 

use:

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

 

 

How to access the old interface:

Old interface content:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325720660&siteId=291194637