查看文章详情
1. 需求分析
2. 选择的实现方案
3. 实现步骤
3.1.在artile微服务中添加MinIO和freemarker的支持
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>com.heima</groupId>
<artifactId>heima-file-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
3. 2 模板文件(article.ftl)拷贝到article微服务下(课程资料,篇幅过长,不宜粘贴)
3.3 index.js和index.css两个文件手动上传到MinIO中(课程资料有)
3.4 新建ApArticleContentMapper
package com.heima.article.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.heima.model.article.pojos.ApArticleContent;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ApArticleContentMapper extends BaseMapper<ApArticleContent> {
}
6.在artile微服务中新增测试类(后期新增文章的时候创建详情静态页,目前暂时手动生成)
package co.heima.test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.ArticleApplication;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.service.ApArticleService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ArticleApplication.class)
public class ArticleFreemarkTest {
@Autowired
private ApArticleContentMapper apArticleContentMapper;
@Autowired
private ApArticleService apArticleService;
@Autowired
private Configuration configuration;
@Autowired
private FileStorageService fileStorageService;
@Test
public void test() throws Exception {
LambdaQueryWrapper<ApArticleContent> lwq = new LambdaQueryWrapper<>();
lwq.eq(ApArticleContent::getArticleId, "1302864436297482242");
ApArticleContent articleContent = apArticleContentMapper.selectOne(lwq);
if(articleContent != null && StringUtils.isNotBlank(articleContent.getContent())){
Template template = configuration.getTemplate("article.ftl");
Map<String,Object> map = new HashMap<>();
map.put(" content", JSON.parseArray(articleContent.getContent()));
StringWriter out = new StringWriter();
template.process(map,out);
InputStream is = new ByteArrayInputStream(out.toString().getBytes(StandardCharsets.UTF_8));
String static_url = fileStorageService.uploadHtmlFile("", articleContent.getArticleId() + ".html", is);
LambdaUpdateWrapper<ApArticle> luw = new LambdaUpdateWrapper<>();
luw.eq(ApArticle::getId, articleContent.getArticleId())
.set(ApArticle::getStaticUrl,static_url);
apArticleService.update(luw);
}
}
}