RestTemplateコールサービス

4026 ワード

テスト-------------------------
package com.example.demo.controller;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * desc:
 * author CDN
 * create 2019-11-06 17:21
 * version 1.0.0
 */
@RestController
@Configuration
public class Ter {
    @Autowired
    private RestTemplate restTemplate;


    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {

        RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);// ms
        factory.setConnectTimeout(5000);// ms
        return factory;
    }

    /**
     * desc:
     * param: []
     * return:
     * author: CDN
     * date: 2019/11/6
     */
    @RequestMapping("qq")
    public Object aa(){
        return startHttpMethod("http://127.0.0.1:8087/api/admin/v1/bid/list?pageIndex=0&pageSize=10",null,"post");
    }


    public String startHttpMethod(String ReqAdderss, List> argMapList, String httpMethod) {
        Map httpMethodMap = new HashMap<>();
        httpMethodMap.put("HEAD", HttpMethod.HEAD);
        httpMethodMap.put("GET", HttpMethod.GET);
        httpMethodMap.put("POST", HttpMethod.POST);
        httpMethodMap.put("PUT", HttpMethod.PUT);
        httpMethodMap.put("PATCH", HttpMethod.PATCH);
        httpMethodMap.put("DELETE", HttpMethod.DELETE);
        httpMethodMap.put("OPTIONS", HttpMethod.OPTIONS);
        httpMethodMap.put("TRACE", HttpMethod.TRACE);
        if (httpMethodMap.get(httpMethod.toUpperCase()) == null) {
            return " ";
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE));
        headers.set(HttpHeaders.ACCEPT_CHARSET, StandardCharsets.UTF_8.toString());
//   , Map HashMap, 
        MultiValueMap params = new LinkedMultiValueMap<>();
//// 
//        params.add("pageIndex", "0");             // 
//        params.add("pageSize", "10");           // 
        if (null !=argMapList && argMapList.size()>0){
            for (Map stringStringMap : argMapList) {
                for (String s : stringStringMap.keySet()) {
                    params.add(s,stringStringMap.get(s));
                }
            }
        }
        HttpEntity> requestEntity = new HttpEntity<>(params, headers);
//   HTTP 
        ResponseEntity response = restTemplate.exchange(ReqAdderss, httpMethodMap.get(httpMethod.toUpperCase()), requestEntity, String.class);  // String.class   
        return response.getBody();
    }


}