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


<!-- Using jersey RESTful framework -->

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;


     * 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){
        }else {

     * 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

        //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)
        T resultObject = response.readEntity(tClass);
        return resultObject;




public ResultBean sendMessage(@Content HttpServletRequest request,@QueryParam("phone") String phone){
    //combine parameters
    Form from = new Form();
    String response = JerseyClientUtil.getJerseyClientUtil()
    ResultBean resultBean  = new ResultBean(response);
    return resultBean;



How to access the old interface:

Old interface content:


Guess you like