springboot为实体追加图片路径

在项目中需要同时返回半路径和全路径两种
思考解决方案,想到的相对合理的方式
1.在application.properties配置图片资源库路径

例:host.url=http://www.baidu.com

2.设置静态属性

    private static String hostUrl = "";

    public static String getHostUrl() {
        return hostUrl;
    }

    public static void setHostUrl(String hostUrl) {
        Constants.hostUrl = hostUrl;
    }

3.配置运行初始化,配静态变量赋值

@Component
public class MyAppRunner implements ApplicationRunner {

    @Value("${host.url}")
    private String hostUrl;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Variable.setHostUrl(hostUrl);
    }
}

4.为实体属性拼接路径

    @ApiModelProperty("头像半路径")
    private String icon;

    @ApiModelProperty("头像全路径")
    private String iconFull;

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getIconFull() {
        return iconFull;
    }

    public void setIconFull() {
        this.iconFull = Variable.getHostUrl() + icon;
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容