【问题标题】: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>";
        }
    }
    

    【讨论】:

    • 是否可以隐藏URL传入的参数
    猜你喜欢
    • 2021-06-16
    • 2014-12-15
    • 2018-10-29
    • 2018-03-17
    • 2014-02-23
    • 2014-12-15
    • 2021-07-15
    • 2017-06-21
    • 2020-04-17
    相关资源
    最近更新 更多