通过@RequestMapping 调用另一个项目 url
Make a call to another project via @RequestMapping url
我想通过调用控制器将应用程序 A 连接到应用程序 B:
应用程序 B URL 是:
@RequestMapping(value = "/v/getInfo")
public ResponseEntity<String> getVInfo() {
vService.getInfo();
return new ResponseEntity<>("Success", HttpStatus.OK);
}
这两个应用程序都是由我们开发的,尚未考虑将两者集成在一起。
这可以吗?我们正在使用 spring 和 Java8,我很困惑我应该从哪里开始。应用程序 B 在从控制器调用 URL 之前也需要身份验证。
更新:
App A 的配置与 App B 相同,将数据保存到数据库后,我们需要调用 App B 来操作这些数据(代码在 App B 中)。基本上在 App A 进程结束时,我们需要启动 App B 进程之一。
您只需要使用标准 RestTemplate
.
进行正常的网络调用
在应用程序 A 的最基本级别:
@Component
public class AppBCaller {
@Autowired RestTemplate template;
public String getInfo() {
String plainCreds = "username:password";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<String> response = restTemplate.exchange("http://whereAppBis/v/getInfo", HttpMethod.GET, request, String.class);
return response.getBody();
}
}
我想通过调用控制器将应用程序 A 连接到应用程序 B: 应用程序 B URL 是:
@RequestMapping(value = "/v/getInfo")
public ResponseEntity<String> getVInfo() {
vService.getInfo();
return new ResponseEntity<>("Success", HttpStatus.OK);
}
这两个应用程序都是由我们开发的,尚未考虑将两者集成在一起。
这可以吗?我们正在使用 spring 和 Java8,我很困惑我应该从哪里开始。应用程序 B 在从控制器调用 URL 之前也需要身份验证。
更新: App A 的配置与 App B 相同,将数据保存到数据库后,我们需要调用 App B 来操作这些数据(代码在 App B 中)。基本上在 App A 进程结束时,我们需要启动 App B 进程之一。
您只需要使用标准 RestTemplate
.
在应用程序 A 的最基本级别:
@Component
public class AppBCaller {
@Autowired RestTemplate template;
public String getInfo() {
String plainCreds = "username:password";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<String> response = restTemplate.exchange("http://whereAppBis/v/getInfo", HttpMethod.GET, request, String.class);
return response.getBody();
}
}