目录

请求第三方接口数据并返回前端

目录

请求第三方接口数据并返回前端

之前做过类似的,这次略微有点不一样,在此记录一下

https://i-blog.csdnimg.cn/blog_migrate/8a03064bc0c61e33ed8dc20212c38c14.png

APP后台请求控制系统的接口,返回的是json数据,把它返回给前端

因为控制系统那边还没完成,这里我试elasticsearch的客户端,请求127.0.0.1:9200来试试

https://i-blog.csdnimg.cn/blog_migrate/cb8bfc0a856a71bc43b216d37f1b1715.png

一般请求地址都是http://127.0.0.1:8080/get_brief_status;

由于有很多个请求,后面请求地址不一样,所以把http://127.0.0.1:8080放到yml文件里,/get_brief_status作为参数在每个接口里单独定义。

这里我把http://127.0.0.1:8080放到yml里 “:9200”作为参数

在application.yml里面配置地址:

https://i-blog.csdnimg.cn/blog_migrate/ad5d8df2309b9e6c8d5c5a536118e03f.png

config配置类MyProps:为了拿到yml里面的url地址

@Component
@ConfigurationProperties(prefix="system")
public class MyProps {
    public String url;
    public String getUrl() {
        return url;
    }
    public MyProps setUrl(String url) {
        this.url = url;
        return this;
    }
}

controller层;

@GetMapping(value = "/getGuidePoints")
    @ApiOperation("获取导引点列表")
    public CommonResult getGuidePoints(){
        JSON json = httpClientService.getResponse(":9200");
        return CommonResult.success(json);
    }

service层:

public interface HttpClientService {

    JSON getResponse(String url);
}

service实现类:

@Service
public class HttpClientServiceImpl implements HttpClientService {

    @Autowired
    private MyProps myProps;

    @Override
    public JSON getResponse(String url) {
        String result = "";
        result= HttpUtil.get(myProps.getUrl()+url);
        JSON json = JSONUtil.parse(result);
        return json;
    }
}

测试: 成功拿到并返回

https://i-blog.csdnimg.cn/blog_migrate/566c31c4960d9ff914a00135b8df9594.png