【问题标题】:Return html content using @Endpoint springboot使用@Endpoint springboot 返回 html 内容
【发布时间】:2021-11-01 13:28:27
【问题描述】:
我可以访问 http://localhost/manage/welcome 页面。但无法获取html。它只是返回字符串。
@Endpoint(id="welcome")
@Component
public class UtilClass {
@ReadOperation
public String logger() {
return "<html><table>sampletable</table></html>";
}
}
请建议任何不使用@Controller/ @RestController 或 thyleaf 的方式来加载 html 内容
【问题讨论】:
标签:
java
spring-boot
spring-boot-actuator
【解决方案1】:
我不明白你为什么不想使用@Controller
@ReadOperation有一个参数可以在spring boot 2.xx中设置输出内容类型。
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
@Endpoint(id = "welcome")
@Component
public class UtilClass {
@ReadOperation(produces = MediaType.TEXT_HTML_VALUE)
public String logger() {
return "<html>" +
"<table>" +
" <thead><tr>" +
" <th>ID</th><th>Name</th></tr>" +
"</thead>" +
" <tbody><tr>" +
" <td>1</td><td>Arfat</td></tr>" +
"</tbody>" +
"</table>" +
"</html>";
}
}