Spring bootで外部URLをどのように転送できますか?
Spring Bootは外部URLを転送するためのいくつかの方法を提供します。
- ネイティブにしか理解できないレストテンプレート
- オブジェクトを取得
- エンティティを取得する
- postForObject()
RestTemplate restTemplate = new RestTemplate();
String url = "http://example.com/external-url";
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
return response.getBody();
- リダイレクト表示
- リダイレクト ビュー
RedirectView redirectView = new RedirectView("http://example.com/external-url");
redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
return new ModelAndView(redirectView);
- HTTPサーブレスポンス
- HTTPレスポンス
- sendRedirect()
@RequestMapping("/external-url")
public void redirectExternalUrl(HttpServletResponse response) throws IOException {
response.sendRedirect("http://example.com/external-url");
}
これらは外部URLを共有する一般的な方法であり、どれを使用するかは要件と状況によって異なります。