美文网首页
RestTemplate

RestTemplate

作者: 我正在看着你 | 来源:发表于2019-01-24 17:20 被阅读0次

package com.croot.ta.controller;

import java.io.IOException;

import java.lang.reflect.Method;

import java.text.SimpleDateFormat;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpMethod;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

import com.croot.ta.entity.TaAccountinfo;

import com.croot.ta.entity.TaSchemePerformance;

import com.croot.ta.rest.Response;

import com.croot.ta.utils.BeanUtil;

import com.croot.ta.utils.DateUtil;

import com.fasterxml.jackson.core.JsonFactory;

import com.fasterxml.jackson.core.JsonParser;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.ObjectMapper;

import net.sf.json.JSONObject;

/**

*  

*

* @author  wulei

* @ClassName: TaAccountInfoController 

* @Description: TA 账号信息

* @date 2018年10月25日 下午1:42:53 

*/

@RestController

@RequestMapping("rest")

public class RestTestController {

    @RequestMapping("/post")

    public Response restpost() {

        // 创建restTemplate

        RestTemplate restTemplate = new RestTemplate();

        // 存放请求参数集合

        Map<String, Object> params = new HashMap<>(3);

        params.put("productNum", "2");

        params.put("productType", "");

        params.put("schemeId", "a2f35119");

        JSONObject fromObject = JSONObject.fromObject(params);

        /*String url = "http://192.168.1.43:8084/TA/accountInfo/getInfoByCustId";*/

        String url = "http://192.168.1.43:8084/TA/scheme/performance/selectDetail";

        HttpHeaders headers = new HttpHeaders();

        headers.add("uuid",

            "crooteyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjcm9vdCIsInN1YiI6Ijk5OTkwIiwiZXhwIjoxNTQ4ODM5NDcyLCJuYmYiOjE1NDgyMzQ2NzJ9.SQznsMmVkeIaRpG_wWeNPSP-dx1_PN3TDyw-lETu3cY");

        headers.add("Content-Type", "application/json");

        headers.set("Accept-Charset", "utf-8");

        headers.set("Content-type", "application/json; charset=utf-8");// 设置编码

        // 请求头带参数

        HttpEntity<Object> entity = new HttpEntity<>(fromObject, headers);

        JSONObject postForObject = restTemplate.postForObject(url, entity, JSONObject.class);

        Object productId = postForObject.getJSONObject("data").get("productId");

        Object productNum = postForObject.getJSONObject("data").get("productNum");

        /*Object object = postForObject.get("data");*/

        Map<String, Method> methodMap = new HashMap<String, Method>();

        BeanUtil.loadMethod(methodMap, TaSchemePerformance.class);

        TaSchemePerformance pojo = new TaSchemePerformance();

        try {

            BeanUtil.setValue(methodMap, pojo, productId.toString(), "productId");

            BeanUtil.setValue(methodMap, pojo, productNum.toString(), "productNum");

            System.out.println(pojo.getProductId());

        } catch (Exception e) {

            // Auto-generated catch block

            e.printStackTrace();

        }

        // 调用

        String result = restTemplate.postForObject(url, entity, String.class);

        System.out.println(result);

        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, String.class, params);

        System.out.println(exchange.getBody().toString());

        /* restTemplate.getForEntity(url, String.class, uriVariables)*/

        /*ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class, entity);

        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, entity, String.class);*/

        return null;

    }

    @RequestMapping("/get")

    public Response restget() {

        // 创建restTemplate

        RestTemplate restTemplate = new RestTemplate();

        // 存放请求参数集合

        Map<String, Object> params = new HashMap<>(3);

        params.put("acctID", "JY1000000004");

        params.put("custID", "200010000007");

        params.put("custFundAcctID", "100020000008");

        String url =

            "http://192.168.8.32:8080/TA/accountInfo/getAccountInfo?acctID={acctID}&custID={custID}&custFundAcctID={custFundAcctID}";

        HttpHeaders headers = new HttpHeaders();

        headers.add("uuid",

            "crooteyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjcm9vdCIsInN1YiI6Ijk5OTkwIiwiZXhwIjoxNTQ4ODEwNzI3LCJuYmYiOjE1NDgyMDU5Mjd9.vAkE0UZNIkMvVnUCD5ijHHFC1Wmo0129FJiYvO9w1cA");

        // 请求头带参数

        HttpEntity<Object> entity = new HttpEntity<>(null, headers);

        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params);

        System.out.println(exchange.getBody().toString());

        return null;

    }

    @RequestMapping("/getlist")

    public Response restgetlist() {

        // 创建restTemplate

        RestTemplate restTemplate = new RestTemplate();

        // 存放请求参数集合

        Map<String, Object> params = new HashMap<>(3);

        String url = "http://192.168.8.32:8080/TA/custInfo/getList";

        HttpHeaders headers = new HttpHeaders();

        headers.add("uuid",

            "crooteyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJjcm9vdCIsInN1YiI6Ijk5OTkwIiwiZXhwIjoxNTQ4ODMzODMwLCJuYmYiOjE1NDgyMjkwMzB9.r7LNzq4YG9t_N6KFqbe901-ip2BW6btxXgK2yImMPBs");

        // 请求头带参数

        HttpEntity<Object> entity = new HttpEntity<>(null, headers);

        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params);

        String body = exchange.getBody();

        JSONObject fromObject = JSONObject.fromObject(body);

        List<?> result = (List<?>)fromObject.get("data");

        for (Object pojo : result) {

            String property;

            try {

                property = BeanUtils.getProperty(pojo, "custName");

                System.out.println(property);

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        System.out.println(body);

        System.out.println(result);

        return null;

    }

    public static void main(String[] args) {

        Boolean ifDate = true;

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");

        try {

            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01

            format.setLenient(false);

            format.parse("2018-01-01 02:03:04");

        } catch (Exception e) {

            ifDate = false;

            e.printStackTrace();

            // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对

        }

        if (ifDate) {

            long millis = DateUtil.getMillis("2018-01-01 02:03:04");

            System.out.println(millis);

        }

    }

}

相关文章

网友评论

      本文标题:RestTemplate

      本文链接:https://www.haomeiwen.com/subject/irdujqtx.html