![](/img/trans.png)
[英]Not able to return error code with message from rest API in Spring boot
[英]How to return html from Rest API with spring boot application
我需要在Spring Boot Application中從Rest API返回html頁面。 html "test.html"
位於src / main / resource目錄中。 以下是我的代碼段
@RequestMapping(value ="/get/getReportById",method = RequestMethod.GET)
@ResponseBody()
public String getReportsByCategory(String id) throws Exception{
try{
//Do something
}catch(Exception e){
e.printStackTrace();
}
return "test";
}
類應該是@Controller,刪除@ResponseBody,還應該配置模板處理器(例如Thymeleaf)。
更新
如果查看@RestController的代碼內部,您將看到它由@Controller和@ResponseBody批注組成。 因此ResponseBody將自動應用於所有方法。
如果要返回視圖,則不想將控制器方法注釋為@ResponseBody,因此請將其刪除,它應該可以工作。
您需要從類路徑中讀取test.html
並將其作為字符串返回:
InputStream htmlStream = getClass().getResourceAsStream("test.html");
return new Scanner(htmlStream, "UTF-8").useDelimiter("\\A").next();
是用來處理Web服務的,這全都與數據有關,諸如HTML之類的表示標記在這里沒有任何作用。
如果涉及到視圖,這就是您要使用的東西。 view可以是吐出html的任何內容(帶有或不帶有模型的插值)。 您可以使用良好的舊JSP,JSF或模板引擎,如百里香葉,速度,freemarker等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.