springboot集成plantuml
Posted 阿拉的梦想
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot集成plantuml相关的知识,希望对你有一定的参考价值。
springboot集成plantuml
当您对PlantUML有了基本的了解, 就可以在本地安装和使用PlantUML了。
软件要求:
Java: 是运行PlantUML的必需条件, 请在您的环境中安装Java。
graphviz-dot: 可选的, 但是建议安装 (如果想绘制除 时序图和活动图以外的图, 就需要安装 Graphviz 软件)。
1. graphviz安装
1.1 windows
自plantuml 1.2020.21以后的jar包中自带了graphviz,无需自己安装。
1.2 Linux
Fedora packages: sudo yum install graphviz
Ubuntu packages: sudo apt install graphviz
Debian packages: sudo apt install graphviz
默认情况下,会安装在下面目录:
Firstly in: /usr/local/bin/dot
Then in: /usr/bin/dot
也可以安装在其他目录,然后用环境变量GRAPHVIZ_DOT
指定GraphViz 的目录.
2. springboot集成plantuml
2.1 添加maven依赖
<!-- https://mvnrepository.com/artifact/net.sourceforge.plantuml/plantuml -->
<dependency>
<groupId>net.sourceforge.plantuml</groupId>
<artifactId>plantuml</artifactId>
<version>1.2022.1</version>
</dependency>
2.2 plantuml配置
plantuml.jar中是使用socket自建服务监听请求的。因此,需要在springboot中使用另一个端口启动plantuml服务。
package com.demo.devops.document.config;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.plantuml.picoweb.PicoWebServer;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
@Slf4j
@Component
public class PlantUmlServer implements ApplicationRunner
@Override
public void run(ApplicationArguments args) throws Exception
startServer(8081,null);
public void startServer(final int port, final String bindAddress) throws IOException
final InetAddress bindAddress1 = bindAddress == null ? null : InetAddress.getByName(bindAddress);
final ServerSocket serverConnect = new ServerSocket(port, 50, bindAddress1);
log.info("webPort=",serverConnect.getLocalPort());
PicoWebServer.serverLoop(serverConnect);
2.3http请求转发
可以直接访问plantuml的端口8081来使用。若是此端口没有映射出来,那么就需要使用springboot来转发请求到plantuml了。
package com.demo.devops.document.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
@RestController
@Slf4j
public class PlantUmlController
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/plantuml/**")
public Object plantUml2(HttpServletRequest request, HttpServletResponse response) throws Exception
String requestURI = request.getRequestURI();
ResponseEntity<String> res= restTemplate.getForEntity("http://localhost:" + 8081 + requestURI, String.class);
HttpHeaders headers = res.getHeaders();
headers.forEach((key, value) ->
response.addHeader(key, value.get(0));
);
//linux中使用时,中文会重叠,改变自适应方式临时解决
String body = res.getBody().replaceAll("\\"spacing\\"", "\\"spacingAndGlyphs\\"");
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
response.setContentLength(bytes.length);
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
out.flush();
return null;
2.4 访问
使用下面测试数据,就能看到效果了
http://localhost:8080/plantuml/svg/~1UDfpA2v9B2efpStXuaeiBSdFAyrDIYq2ifDByeiKb9vFMV6iOxkIGcAnGcWnKBm0g3OpERCWCQz4GUdPrluNopkUpbm1bdSgoY_7L3AdzsdNmkUhruEL1ASMbQKMAIZd5oNcfQU2BSaiIon9XPZD1I8LTErW9baflDWt-MdhKeNG81-4KfWrN506I0F4NA3wzTkVRMo2s0bq5a8JH06N0XVK1KXwdgrO-6pk_cSxDZsRFvahBn-ek2WpFQD48JyDOUp-cSzch7-oe-79ZbNFDgzusTx5XMGGmhNxfpoTFK_18yJNXoib5TrkX7k1UY3-TKZDIm46F00hoPzX
plantuml语法 参考 https://plantuml.com/zh/
以上是关于springboot集成plantuml的主要内容,如果未能解决你的问题,请参考以下文章