news 2026/4/16 21:51:41

SpringBoot项目集成ONLYOFFICE

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot项目集成ONLYOFFICE

ONLYOFFICE 文档8.2版本已发布:PDF 协作编辑、改进界面、性能优化、表格中的 RTL 支持等更新

文章目录
  • 前言
  • ONLYOFFICE 产品简介
  • 功能与特点
  • Spring Boot 项目中集成 OnlyOffice
    • 1. 环境准备
    • 2. 部署OnlyOffice Document Server
    • 3. 配置Spring Boot项目
    • 4. 实现文档编辑功能
    • 5. 文档保存与版本控制
      • (1)配置文件 `application.properties`
      • (2)依赖管理 `pom.xml`
      • (3)控制器 `DocumentController.java`
      • (4)配置类 `OnlyOfficeConfig.java`
      • (5)文档服务类 `DocumentService.java`
      • (6)文档模型类 `Document.java`
      • (7)前端页面 `index.html`
      • (8)用户服务类 `UserService.java`(可选)
      • (9)修改控制器以添加权限验证 `DocumentController.java`
    • 6. 安全性和权限管理
  • 体验与测评
  • 8.2 版本的其他更新:
  • 结尾:ONLYOFFICE 项目介绍

前言

提示:这里可以添加本文要记录的大概内容:

随着互联网技术的发展,越来越多的企业和个人开始寻求高效的在线文档处理解决方案。传统的本地文档编辑软件虽然功能强大,但在多用户协同工作方面存在诸多不便。为了满足这一需求,市场上涌现出了许多优秀的在线文档编辑工具,其中OnlyOffice因其出色的性能和灵活的集成能力而受到广泛好评。本文将详细介绍如何在Spring Boot项目中集成OnlyOffice,实现文档的在线编辑功能,并分享OnlyOffice的产品特点和用户体验。

ONLYOFFICE 产品简介

OnlyOffice是一套完整的开源办公套件,旨在为企业和个人提供高效的文档处理解决方案。它包含文字处理、电子表格和演示文稿三种类型的文档编辑器,支持多种文档格式的读取和编辑。OnlyOffice不仅提供了丰富的桌面应用程序,还拥有强大的Web版编辑器——OnlyOffice Document Server,后者可以方便地集成到各类Web应用中,实现文档的在线编辑和协作。

功能与特点

  • 全面的文档支持:支持DOCXXLSXPPTX等多种文档格式的编辑和转换。
  • 实时协作编辑:允许多个用户同时编辑同一文档,所有更改实时同步。
  • 权限管理:提供细粒度的文档访问权限控制,确保文档的安全性。
  • 版本管理:自动记录文档的历史版本,便于追踪修改记录和恢复早期版本。
  • 插件扩展:支持通过插件扩展编辑器的功能,满足更多个性化需求。
  • API接口丰富:提供RESTful APIJavaScript API,方便开发者集成到现有系统中。

Spring Boot 项目中集成 OnlyOffice

1. 环境准备

  • Java环境:确保已经安装了Java环境(建议JDK 1.8及以上版本)。

  • 构建工具:安装MavenGradle作为项目的构建工具。

  • Spring Boot项目:可以通过Spring Initializr快速创建一个Spring Boot项目基础框架。

2. 部署OnlyOffice Document Server

  • 安装OnlyOffice:可以选择在本地或云服务器上安装OnlyOffice Document Server。官方提供了详细的安装指南,可以根据自己的操作系统选择合适的安装方法。

  • 检查安装:安装完成后,确保Document Server能够正常访问,通常可以通过浏览器访问http://<your_server_ip>:80来检查是否成功安装。

3. 配置Spring Boot项目

  • 添加配置:在Spring Boot项目的application.properties文件中添加OnlyOffice服务器的相关配置信息,如服务器地址等。

    onlyoffice.server.url=http://<your_server_ip>
    onlyoffice.server.secret=<your_secret_key>

  • 创建控制器:创建一个控制器(Controller)用于处理前端请求,调用OnlyOffice API进行文档的加载、保存等操作。

    @RestController
    @RequestMapping(“/documents”)
    public class DocumentController {

    @Value("${onlyoffice.server.url}") private String serverUrl; @Value("${onlyoffice.server.secret}") private String secretKey; @GetMapping("/{id}") public ResponseEntity<?> getDocument(@PathVariable String id) { // 调用OnlyOffice API获取文档编辑所需参数 // ... } @PostMapping("/{id}") public ResponseEntity<?> saveDocument(@PathVariable String id, @RequestBody Document document) { // 处理文档保存逻辑 // ... }

    }

4. 实现文档编辑功能

  • 前端集成:利用OnlyOffice提供的JavaScript API,在前端页面中嵌入文档编辑器。

5. 文档保存与版本控制

  • 保存文档:当用户完成编辑并保存文档时,OnlyOffice会将更新后的文档发送回指定的回调URL。

    @PostMapping(“/{id}”)
    public ResponseEntity<?> saveDocument(@PathVariable String id, @RequestBody Document document) {
    // 将文档保存到数据库或文件系统中
    // 记录版本信息
    return ResponseEntity.ok().build();
    }

  • 版本管理:记录文档的历史版本,便于追踪修改记录和恢复早期版本。

以下是Spring Boot项目中集成OnlyOffice的完整代码部分:

(1)配置文件application.properties
onlyoffice.server.url=http://<your_server_ip> onlyoffice.server.secret=<your_secret_key>
(2)依赖管理pom.xml
<dependencies> <!-- Spring Boot Web Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Thymeleaf Starter (可选,用于模板引擎) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Spring Boot Security Starter (可选,用于安全控制) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- JSON处理库 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> </dependencies>
(3)控制器DocumentController.java
package com.example.onlyoffice.controller; import com.example.onlyoffice.config.OnlyOfficeConfig; import com.example.onlyoffice.model.Document; import com.example.onlyoffice.service.DocumentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/documents") public class DocumentController { @Autowired private OnlyOfficeConfig onlyOfficeConfig; @Autowired private DocumentService documentService; @GetMapping("/{id}") public ResponseEntity<Document> getDocument(@PathVariable String id) { Document document = documentService.getDocumentById(id); if (document == null) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok(document); } @PostMapping("/{id}") public ResponseEntity<Void> saveDocument(@PathVariable String id, @RequestBody Document document) { documentService.saveDocument(id, document); return ResponseEntity.ok().build(); } @GetMapping("/config/{id}") public ResponseEntity<Object> getConfig(@PathVariable String id) { return ResponseEntity.ok(onlyOfficeConfig.getConfig(id)); } }
(4)配置类OnlyOfficeConfig.java
package com.example.onlyoffice.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; @Component public class OnlyOfficeConfig { @Value("${onlyoffice.server.url}") private String serverUrl; @Value("${onlyoffice.server.secret}") private String secretKey; public Map<String, Object> getConfig(String documentId) { Map<String, Object> config = new HashMap<>(); config.put("document", Map.of( "fileType", "docx", "key", documentId, "title", "Sample Document", "url", serverUrl + "/documents/" + documentId )); config.put("documentType", "word"); config.put("editorConfig", Map.of( "callbackUrl", serverUrl + "/documents/" + documentId, "mode", "edit", "lang", "en" )); config.put("customization", Map.of( "actionBar", false, "chat", false )); return config; } }
(5)文档服务类DocumentService.java
package com.example.onlyoffice.service; import com.example.onlyoffice.model.Document; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.ArrayList; import java.util.Collections; @Service public class DocumentService { private final Map<String, Document> documents = new HashMap<>(); private final Map<String, List<Document>> history = new HashMap<>(); public Document getDocumentById(String id) { return documents.get(id); } public void saveDocument(String id, Document document) { Document currentDocument = documents.get(id); if (currentDocument != null) { history.computeIfAbsent(id, k -> new ArrayList<>()).add(currentDocument); } documents.put(id, document); } public List<Document> getHistory(String id) { return history.getOrDefault(id, Collections.emptyList()); } }
(6)文档模型类Document.java
package com.example.onlyoffice.model; import lombok.Data; @Data public class Document { private String content; // 其他字段... }
(7)前端页面index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>OnlyOffice Document Editor</title> </head> <body> <div id="document-editor" style="height: 600px;"></div> <script src="https://<your_server_ip>/web-apps/apps/api/documents/api.js"></script> <script> async function loadDocumentEditor() { const response = await fetch('/documents/config/<document_id>'); const config = await response.json(); new DocsAPI.DocEditor("document-editor", config); } window.onload = loadDocumentEditor; </script> </body> </html>
(8)用户服务类UserService.java(可选)
package com.example.onlyoffice.service; import org.springframework.stereotype.Service; @Service public class UserService { public boolean hasAccess(String username, String documentId) { // 这里实现具体的权限验证逻辑 // 示例:假设所有用户都有访问权限 return true; } }
(9)修改控制器以添加权限验证DocumentController.java
package com.example.onlyoffice.controller; import com.example.onlyoffice.config.OnlyOfficeConfig; import com.example.onlyoffice.model.Document; import com.example.onlyoffice.service.DocumentService; import com.example.onlyoffice.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/documents") public class DocumentController { @Autowired private OnlyOfficeConfig onlyOfficeConfig; @Autowired private DocumentService documentService; @Autowired private UserService userService; @GetMapping("/{id}") public ResponseEntity<Document> getDocument(@PathVariable String id, @AuthenticationPrincipal UserDetails userDetails) { if (!userService.hasAccess(userDetails.getUsername(), id)) { return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); } Document document = documentService.getDocumentById(id); if (document == null) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok(document); } @PostMapping("/{id}") public ResponseEntity<Void> saveDocument(@PathVariable String id, @RequestBody Document document, @AuthenticationPrincipal UserDetails userDetails) { if (!userService.hasAccess(userDetails.getUsername(), id)) { return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); } documentService.saveDocument(id, document); return ResponseEntity.ok().build(); } @GetMapping("/config/{id}") public ResponseEntity<Object> getConfig(@PathVariable String id, @AuthenticationPrincipal UserDetails userDetails) { if (!userService.hasAccess(userDetails.getUsername(), id)) { return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); } return ResponseEntity.ok(onlyOfficeConfig.getConfig(id)); } }

以上代码涵盖了从配置文件、依赖管理、控制器、配置类、服务类、模型类到前端页面的完整实现。希望这些代码能帮助你在Spring Boot项目中成功集成OnlyOffice。如果有任何问题或需要进一步的帮助,请随时提问。

6. 安全性和权限管理

  • 权限控制:通过OnlyOffice提供的API设置文档的访问权限,例如只读、编辑等。

  • 后端验证:在Spring Boot后端实现相应的权限验证逻辑,确保只有授权用户才能访问特定的文档。

体验与测评

在实际使用中,OnlyOffice的表现令人满意。其界面设计简洁直观,用户可以轻松上手;编辑器响应速度快,即使在网络条件不佳的情况下也能保持流畅的操作体验。OnlyOffice对中文文档的支持也相当不错,无论是字体显示还是排版布局都达到了较高的水平。

  • 更快的文件加载速度:为了加快编辑器的打开速度,我们优化了加载脚本。与之前的版本相比:

打开普通文件 – 最高提速21%
打开演示文稿 – 最高提速17%

Excel:

PPT:

PDF:

当然也有一些不足之处需要注意。部分高级功能需要购买商业许可证才能使用,这可能会增加企业的成本负担。另外与其他一些在线文档编辑工具相比,OnlyOffice的社区活跃度稍显不足,遇到问题时可能难以获得及时的帮助和支持。

8.2 版本的其他更新:

协作编辑 PDF 文件;

文本文档中的域代码;

从第三方来源插入文本;

预设阿拉伯语数字编号;

电子表格中的迭代计算;

电子表格编辑器中的丝滑滚动;

在幻灯片上绘图;

演示文稿中的随机切换效果;

所有语言的词典更新和拼写检查改进;

新的图表类型,如直方图、瀑布图、漏斗图等。

结尾:ONLYOFFICE 项目介绍

OnlyOffice不仅仅是一款文档编辑工具,它背后是一个充满活力的开源项目。该项目始于2009年,由Ascensio System SIA公司发起并维护。多年来,OnlyOffice不断发展壮大,形成了一个包括文档编辑器、邮件客户端、项目管理等多个子项目的生态系统。目前,OnlyOffice在全球范围内拥有数百万用户,得到了广泛的认可和好评。

ONLYOFFICE 官网

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 14:26:22

思源宋体终极使用手册:从零开始快速美化你的开发环境

思源宋体终极使用手册&#xff1a;从零开始快速美化你的开发环境 【免费下载链接】source-han-serif-ttf Source Han Serif TTF 项目地址: https://gitcode.com/gh_mirrors/so/source-han-serif-ttf 思源宋体作为一款由Adobe与Google联合开发的开源编程字体&#xff0c;…

作者头像 李华
网站建设 2026/4/16 0:50:23

Unity Mod Manager终极教程:新手也能轻松掌握的游戏模组管理神器

Unity Mod Manager终极教程&#xff1a;新手也能轻松掌握的游戏模组管理神器 【免费下载链接】unity-mod-manager UnityModManager 项目地址: https://gitcode.com/gh_mirrors/un/unity-mod-manager 想要为Unity游戏添加新功能却担心操作复杂&#xff1f;Unity Mod Mana…

作者头像 李华
网站建设 2026/4/16 11:02:58

Mac NTFS读写困境的破局之道:开源工具实战指南

Mac NTFS读写困境的破局之道&#xff1a;开源工具实战指南 【免费下载链接】Free-NTFS-for-Mac Nigate&#xff0c;一款支持苹果芯片的Free NTFS for Mac小工具软件。NTFS R/W for macOS. Support Intel/Apple Silicon now. 项目地址: https://gitcode.com/gh_mirrors/fr/Fre…

作者头像 李华
网站建设 2026/4/16 12:46:54

YimMenu完全掌握指南:从入门到精通的GTA5辅助工具使用教程

YimMenu完全掌握指南&#xff1a;从入门到精通的GTA5辅助工具使用教程 【免费下载链接】YimMenu YimMenu, a GTA V menu protecting against a wide ranges of the public crashes and improving the overall experience. 项目地址: https://gitcode.com/GitHub_Trending/yi/…

作者头像 李华
网站建设 2026/4/15 16:32:59

PDF-Extract-Kit实战:批量处理1000+PDF文档的完整流程

PDF-Extract-Kit实战&#xff1a;批量处理1000PDF文档的完整流程 1. 引言&#xff1a;为什么需要PDF智能提取工具&#xff1f; 在科研、工程和企业文档管理中&#xff0c;PDF格式因其跨平台兼容性和版式稳定性成为主流。然而&#xff0c;当面对1000份学术论文、技术报告或扫描…

作者头像 李华
网站建设 2026/4/16 2:26:03

Mac系统NTFS写入限制的技术突破与解决方案

Mac系统NTFS写入限制的技术突破与解决方案 【免费下载链接】Free-NTFS-for-Mac Nigate&#xff0c;一款支持苹果芯片的Free NTFS for Mac小工具软件。NTFS R/W for macOS. Support Intel/Apple Silicon now. 项目地址: https://gitcode.com/gh_mirrors/fr/Free-NTFS-for-Mac …

作者头像 李华