java跨项目调用接口_使用SpringBoot跨系统调用接口的方案

一、简介项目开发中存在系统之间互调问题,又不想用dubbo,这里提供几种springboot方案:1、使用Feign进行消费(推荐)2、使用原始httpClient请求3、使用RestTemplate方法二、方案方案一:使用Feign进行消费(推荐)1、在maven中添加依赖org.springframework.cloudspring-cloud-starter-openfeign2.2.22、...
摘要由CSDN通过智能技术生成

一、简介

项目开发中存在系统之间互调问题,又不想用dubbo,这里提供几种springboot方案:

1、使用Feign进行消费(推荐)

2、使用原始httpClient请求

3、使用RestTemplate方法

二、方案

方案一:使用Feign进行消费(推荐)

1、在maven中添加依赖

org.springframework.cloud

spring-cloud-starter-openfeign

2.2.2

2、启动类上加上@EnableFeignClients

@EnableHystrix

@EnableDiscoveryClient

@EnableFeignClients(basePackages = {"com.aaa.aurora"})

@SpringBootApplication

@EnableTransactionManagement

@ComponentScan(basePackages = "com.aaa.aurora")

@ImportResource(locations= {"classpath:spring.xml","spring-security.xml"})

@MapperScan("com.aaa.aurora.mapper")

public class AuroraWebApplication {

public static void main(String[] args) {

SpringApplication.run(AuroraWebApplication.class, args);

}

}

3、编写service接口

@FeignClient(url = "${pangu.url}",name = "panguUrl")

public interface PanGuService {

@RequestMapping(value = "/pangu/restful/check",method = RequestMethod.POST)

JSONObject check(@RequestParam(name="queryEngine") String queryEngine, @RequestParam(name="querySql") String querySql, @RequestParam(name="jobNo") String jobNo);

}

其中:pangu.url是配置在application.properties中的ip及端口

pangu.url = 192.168.1.3:8080

/pangu/restful/check是要调的接口名

4、代码中调用

@Autowired

private PanGuService panGuService;

JSONObject jsonObject = null;

try {

jsonObject = panGuService.auroraPriviledge(PRESTO_DRIVER, query.get("sql"), user.getWorkNo());

} catch (Exception e) {

throw new Exception("请求系统异常");

}

if (PANGU_FAIL.equals(jsonObject.get("code"))) {

LOG.info(jsonObject.get("msg").toString());

throw new BusinessException(jsonObject.get("msg").toString());

}

方案二:使用原始httpClient请求

使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

1. 创建HttpClient对象。

2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。

3. 如果需要发送请求参数,可调用Http

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,使用RESTful API实现项目调用的方式比较常见。下面给出一个简单的示例: 假设有两个项目,一个是提供方项目,另一个是调用项目。提供方项目中有一个接口提供了查询用户信息的服务,调用项目需要调用这个服务来获取用户信息。 提供方项目中的UserController.java代码如下: ```java @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } } ``` 调用项目中的UserService.java代码如下: ```java @Service public class UserService { @Autowired private RestTemplate restTemplate; public User getUserById(Long id) { String url = "http://localhost:8080/api/users/" + id; return restTemplate.getForObject(url, User.class); } } ``` 在调用项目中,我们注入了一个RestTemplate实例,这个实例可以用来发送HTTP请求。在getUserById方法中,我们构建了一个GET请求的URL,并使用RestTemplate发送这个请求,得到了提供方项目返回的User对象。 需要注意的是,这里的URL是硬编码的,如果提供方项目的地址发生了变化,我们需要手动修改这个URL。在实际开发中,可以将这个URL配置到配置文件中,这样就可以方便地进行修改。 同时,为了让RestTemplate能够正常工作,我们需要在调用项目的配置文件中添加如下配置: ```yaml spring: main: allow-bean-definition-overriding: true application: name: consumer-service restTemplate: rootUri: http://localhost:8080 ``` 这样,我们就可以在调用项目调用提供方项目接口了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值