RestTemplate異常no suitable HttpMessageConverter found for request type [java.lang.Integer]

 GET方式,參數必須放在URL後面,http://xxx/list?name={name}&age={age}java

package com.chelizi.xiruo.xframework.util;

import net.sf.json.JSONObject;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;

/**
 * RestTemplate請求工具類
 * Created by huangbaidong
 * 2017/10/22.
 */
public class RestTemplateRequestUtil {


    /**
     * GET請求
     * @param url
     * @param params
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getRequest(String url, Map<String, Object> params, Class<T> clazz) {
        return request(url, params, clazz, HttpMethod.GET);
    }

    /**
     * POST請求
     * @param url
     * @param params
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T postRequest(String url, Map<String, Object> params, Class<T> clazz) {
        return request(url, params, clazz, HttpMethod.POST);
    }


    /**
     * restTemplate請求,
     * 默認設置了鏈接超時,請求超時時間
     * @param url 請求地址
     * @param params 請求參數
     * @param clazz  返回結果類型
     * @param <T>
     * @return
     */
    public static <T> T request(String url, Map<String, Object> params, Class<T> clazz, HttpMethod method) {
        return request(url, params, clazz, method,30000, 20000);
    }

    /**
     * restTemplate請求
     * @param url 請求地址
     * @param params 請求參數
     * @param clazz  返回結果類型
     * @param readTimeout     請求響應超時時間
     * @param connectTimeout  鏈接超時時間
     * @param <T>
     * @return
     */
    public static <T> T request(String url, Map<String, Object> params, Class<T> clazz,
                                HttpMethod method,int readTimeout, int connectTimeout) {

        RestTemplate restTemplate = new RestTemplate();

        //設置鏈接參數
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setReadTimeout(readTimeout);
        requestFactory.setConnectTimeout(connectTimeout);
        restTemplate.setRequestFactory(requestFactory);
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler());

        //拿到access_toeken
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();

        //請求頭
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.add("Authorization", "bearer "+details.getTokenValue());

        //發起請求
        ResponseEntity<T> responseEntity = null;
        if(HttpMethod.POST.equals(method)) {
            JSONObject jsonParams = JSONObject.fromObject(params);
            HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(jsonParams, headers);
            responseEntity = restTemplate.exchange(url, method, requestEntity, clazz);
        } else {
            HttpEntity requestEntity = new HttpEntity(null, headers);
            responseEntity = restTemplate.exchange(appendParams(url, params.keySet()), method, requestEntity, clazz, params);
        }
        return responseEntity.getBody();
    }


    /**
     * 將參數追加到URL後邊
     * @param url
     * @param keys
     * @return
     * @author: huangbaidong
     * @Description: TODO
     * @return: String
     */
    private static String appendParams(String url, Set<?> keys) {
        StringBuilder sb = new StringBuilder(url);
        if(url.contains("?")) {
            sb.append("&");
        } else {
            sb.append("?");
        }
        for (Object key : keys) {
            sb.append(key).append("=").append("{").append(key).append("}").append("&");
        }
        return sb.deleteCharAt(sb.length() - 1).toString();
    }

}
相關文章
相關標籤/搜索