访问外部系统接口

1.先在实体类中新建一个类MenuInfo:类的字段和json字段一致,而且类型也要考虑,并生成getter和setter



2.准备玩基础工作,进行数据获取,用get形式获取数据,在url里面进行参数传递(get是通过url传递参数)

新建一个controller:GetMenuInfoController

添加注解@ResetController

添加注解@RequestMapping,添加List<MenuInfo>函数(因为返回了8个对象,所以要用List)

package com.zju.visualization.web;

import com.zju.visualization.entity.MenuData;
import com.zju.visualization.entity.MenuInfo;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
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.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value="/jsonTest")
public class GetMenuInfoController {
    private RestTemplate restTemplate = new RestTemplate();

    public List<MenuInfo> postTest(int menuKind){
        MultiValueMap<String,String> headers = new LinkedMultiValueMap<String,String>();
        headers.add("Accept","application/json");
        headers.add("Content-Type","application/json");
        ParameterizedTypeReference<List<MenuInfo>> responseType = new ParameterizedTypeReference<List<MenuInfo>>() {
        };
        Map<String, String> aa = new HashMap<String, String>();
        aa.put("menuKind","2");
        HttpEntity<Object> requestEntiyt = new HttpEntity<Object>(JSONObject.fromObject(aa),headers);
        ResponseEntity<List<MenuInfo>> responseEntity = restTemplate.exchange("http://192.168.0.101:8080/ICP-Management/menu/getMenu.do",
                HttpMethod.POST,requestEntiyt,responseType);
        List<MenuInfo> list = responseEntity.getBody();
        return null;
    }
    @RequestMapping(value="/getMenuInfo")
    public List<MenuInfo> getMenuInfo(int menuKind){

        //get部分
        //用LinkedMultiValueMap数据结构添加header
        MultiValueMap<String,String> headers = new LinkedMultiValueMap<String,String>();
        headers.add("Accept","application/json");
        headers.add("Content-Type","application/json");
        //ParameterizedTypeReference指定响应的对象类型 String:ParameterizedTypeReference<String> responseType = new ParameterizedTypeReference<String>>()
        ParameterizedTypeReference<List<MenuInfo>> responseType = new ParameterizedTypeReference<List<MenuInfo>>() {
        };
        //请求类型  把headers放入HttpEntity请求类型中
        HttpEntity<Object> requestEntiyt = new HttpEntity<Object>(headers);

        String verison = "none";
        //响应实体
        ResponseEntity<List<MenuInfo>> responseEntity = restTemplate.exchange("http://tradehelp.hundsun.com/menu/getMenu.do?version={version}&menuKind={menuKind}",
                HttpMethod.GET,requestEntiyt,responseType,verison,menuKind);
        List<MenuInfo> list = responseEntity.getBody();

        return list;
    }

    private String makeUrl(Map<String,Object> paramsMap){
        StringBuilder paramString = new StringBuilder("");
        for (Map.Entry<String,Object> entry:
                paramsMap.entrySet()) {
            paramString.append(entry.getKey());
            paramString.append("=");
            paramString.append(String.valueOf(entry.getValue()));
            paramString.append("&");
        }
        String result = paramString.toString();
        if(result.endsWith("&"));
        return result.substring(0,result.length());
    }
}

JSONObject需要在pom.xml中添加依赖项:

<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
</dependency>








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值