版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xyy1028/article/details/80672788
1.SpringBoot框架测试该接口
@Autowired
private ITestService testService;
@GetMapping("/test")
public Map test(@RequestParam String name) {
Map a = null;
try {
a = testService.outSideCall(name);
} catch (Exception e) {
e.printStackTrace();
}
return a;
}
2.利用HttpGet访问外部接口url
@Value("${get_url}")
private String path;
public Map outSideCall (String name) throws Exception {
InputStream is = null;
String body = null;
StringBuilder res=new StringBuilder();
Map returnMap = new HashMap<>();
String url=path+
"name="
+name;
HttpGet get = new HttpGet(url);
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
try{
response = client.execute(get);
RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).build();
get.setConfig(config);
HttpEntity entity = response.getEntity();
if(entity != null){
is = entity.getContent();
//转换为字节输入流
BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
while((body=br.readLine()) != null){
res.append(body);
}
}
returnMap = (Map) JSON.parse(res.toString());
System.out.println(returnMap);
} catch (Exception e) {
e.printStackTrace();
}
return returnMap;
}
3配置文件配置好url路径
spring.datasource.url=jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123zxcv
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:mybatis/*.xml
server.port=8083
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
//配置路径
get_url=http://www.why.index?