からHTML応答を返したい
@RequestMapping("/test")
public String testMe(Model model, @RequestParam("param1") String param1, @RequestParam("param2") String param2)
{
//Make a Http call to an internal service and return the response from that call
return "<RESPONSE_FROM_THAT_CALL>";
}
コードの例です:
@Controller
public class MyController {
private RestTemplate restTemplate = new RestTemplate();
@ResponseBody
@RequestMapping("/test")
public String testMe(Model model, @RequestParam("param1") String param1, @RequestParam("param2") String param2) {
URI uri = UriComponentsBuilder.fromHttpUrl("http://www.example.com");
.queryParam("param1", param1)
.queryParam("param2", param2)
.build()
.toUri());
return restTemplate.getForObject(uri, String.class);
}
}
他のサービスにプロキシを追加するエンドポイントがある場合は、たとえばマイクロプロキシとしてのZuul。例えば、このようなプロキシを簡単に作成する方法については、this blog postを参照してください。
あなたの質問は文字列を返す方法ですか? – zeroflagL
もし私が文字列を返すと、springはその名前をビューとエラーとみなします。私は応答の一部としてHTMLを返すようにします – Gowtham
HTML文書は文字列ですね。メソッドに '@ ResponseBody'というアノテーションが付いている場合、Springはビューではなく文字列を返します。 – zeroflagL