####接口部分:使用框架SpringBoot:
代码附上:
**
springboot 接口 不带参数的get请求
@RestController
public class CapPromoteController {
@Autowired
private CapPromoteServiceImpl capPromoteService;
@GetMapping("/cap")
public List<CapPromote> getCapPromotes(){
List<CapPromote> list=capPromoteService.getCapPromotes();
return list;
}
}
###其他项目则使用HttpClient调用
public class HttpClientT {
public static final String ENCODING = "UTF-8";
//相当于浏览器打开
public static CloseableHttpClient createHttpClient() {
CloseableHttpClient httpclient = HttpClients.createDefault();
return httpclient;
}
//Get请求 接口 接收json格式数据
public static void getJson() {
//把访问接口的数据放在集合里
List<Map<Object, Object>> list1=new ArrayList<Map<Object,Object>>();
CloseableHttpClient closeableHttpClient = createHttpClient();
HttpGet httpGet=new HttpGet("http://localhost:8080/cap");
try {
CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result+": 555");
JSONArray jsonArray=JSONArray.fromObject(result);
System.out.println(jsonArray.size());
if(jsonArray.size() > 0){
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject object=(JSONObject)jsonArray.get(i);
//将接口的json数据转换为map值
Map<Object, Object> capMap=object;
list1.add(capMap);
}
}
//测试是否把projPromoteNew表数据装在集合里
System.out.println("list1.size():"+list1.size());
for (Map<Object, Object> map1: list1) {
System.out.println("id:"+map1.get("id"));
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}